Wednesday, November 11, 2015

Salesforce AJAX Toolkit update call fails *silently* with field access issue

Some simple button code with Salesforce Ajax Toolkit (connection.js) to send a lead to queue (and related assignment logic in Apex):

var updateRecord = new Array();
var myquery = "SELECT Id, CompleteOwner__c FROM Lead WHERE Id = '{!Lead.Id}' limit 1";

var result = sforce.connection.query(myquery);
var records = result.getArray("records");

if (records[0] && (records[0].CompleteOwner__c === undefined || records[0].CompleteOwner__c === null)) {
    var update_lead = records[0];

        update_lead.OwnerId = "{!$Setup.CompleteQId__c.Id__c}"; /* To complete Q */

        var callback = {onSuccess: updated, onFailure: failed}
        sforce.connection.update(updateRecord, callback);
The trouble is, the update call returned successfully, but update didn't happen at all - I couldn't even capture the API call with a debug log.

It turns out the trouble is the CompleteOwner__c field.  If that's taken out, the code works fine.  The user has no write permission to the field, but, the code isn't updating it either.  Apparently merely having that field member in the record data structure prevents the update from taking place, without any failure message.  Changing the highlighted part to this, which uses an new object for update without that field member, fixed the problem.
var update_lead = new sforce.SObject("Lead"); //can't use query result for update = "{!Lead.Id}";

update_lead.OwnerId = "{!$Setup.CompleteQId__c.Id__c}"; /* To complete Q */
 The frustrating part is the silent failure.  Why not just fail the call when there's a non-writeable (by permission) field? 

No comments:

Post a Comment