I am trying to write the following code in a javascript button of case object


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

alert('After Case------'+caseObject.Do_you_currently_have_access+':'+caseObject.Status+':'+caseObject.id);

if(caseObject.Do_you_currently_have_access == true && caseObject.Status == 'Closed'){
caseObject.Checkbox_send_Email = true;
var result = sforce.connection.update([caseObject]);

alert('After If------');

What is happening is that in the first alert the id is getting printed. In the second alert the id is getting printed. however the other two values are not being printed(though the record has those values populated). It is showing as undefined. I am fairly new in this. Can anyone tell me where am I going wrong?


Let's take a look at the following snippet of your code:

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

You have declared a new instance caseObject, you have assigned its Id. Have you assigned any value to Do_you_currently_have_access_to_POS__c field of this new instance? No!

True, you have the value set to the field Do_you_currently_have_access_to_POS__c in the database, but it is not going to populate in the new instance you created in your JavaScript magically. You will have to query it and assign it. Instead of creating a blank new instance of case in JavaScript, do something like this:

var caseRecords = sforce.connection.query("SELECT Id, Do_you_currently_have_access_to_POS__c from Case where id ='{!Case.Id}' limit 1"); //add any other fields that you want.
var records = caseRecords.getArray("records"); 
var caseObject = records[0]; 

Now you will have the Do_you_currently_have_access_to_POS__c field value in the caseObject instance.

  • 1
    You really don't need to waste an API call for that, though.
    – sfdcfox
    Apr 18 '17 at 14:50
  • Yes, that's absolutely right. Why I didnt think of that! Thank you. Apr 18 '17 at 15:20

Custom fields always end in __c. Aside from that, you can just use some merge fields for most of your logic:


if({!Case.Do_you_currently_have_access__c && ISPICKVAL(Case.Status, 'Closed')}) {
    var caseRecord = new sforce.SObject("Case");
    caseRecord.Id = "{!Case.Id}";
    caseRecord.Checkbox_Send_Email__c = true;
    var result = sforce.connection.update([caseRecord]);
    if(!result[0].getBoolean("success")) {
        alert("Error: " + result[0]);

You do not need "/soap/ajax/39.0/apex.js" unless you're using sforce.apex.execute.

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.