I used this post to create a button which sets a checkbox field on the Case object (see below). I wish to add simple error handling by examining the return value "result". What's a simple way to parse result for success:'true' or success:'false'?

var c = new sforce.SObject("Case"); 
c.id ="{!Case.Id}"; 

c.User_Request_To_Close__c = true; 
result = sforce.connection.update([c]); 
alert("Request sent with result="+result);

Note: When this succeeds the alert shows:

result={id:'salesforce ID', success:'true',}

When this fails the alert shows:

result={errors:{blah blah blah}, id:null, success:'false',}

I tried using JSON.parse(result) as suggested here but it barfed with the error "JSON Parse error: Expected '}'" I'm not entirely sure why except there seems to be a dangling "," at the end of the return.

Insights appreciated!


The Ajax Toolkit has some basic error handling in its documentation examples as well as in the error handling section of the docs, they are minimal but worth a look.

A basic mechanism for error handling can look like this, no need to use the JSON.parse() function if you don't want to:

// execute the update operation
var response = sforce.connection.update([c]);

if (response[0].getBoolean('success')) {
    // do other successful things

} else {
    alert('An error occurred updating this record:\n\n' + response[0].errors.message);
  • Yep, that works! The samples is a useful page. connection.js source indicates the update() method could also take callbacks for errors as well but that seemed like overkill... – Scott Jun 13 '14 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.