2

I am trying to create a custom button on the detail page of the Account object. The button will simply toggle a checkbox field from true to false and vice versa. The code below works for the "IF" condition, but it does nothing for the "ELSE" condition. If I switch the 2, it also works. Does anyone know why my ELSE statement is being ignored when the button is clicked?

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var updateRecords = []; 
var a = new sforce.SObject("Account"); 
a.id ="{!Account.Id}";

if(a.No_Parent_Account__c = "true"){
a.No_Parent_Account__c = "false";
}else{
a.No_Parent_Account__c = "true";
}
newRecords.push(a); 
result = sforce.connection.update(updateRecords); 
window.location.reload();

2 Answers 2

5

You need to specify that field, or it won't be available to you in your code. Do this instead:

{!RequireScript("/soap/ajax/35.0/connection.js")}
var a = new sforce.SObject("Account");
a.id = "{!Account.Id}";
a.No_Parent_Account__c = !{!Account.No_parent_Account__c};
sforce.connection.update([a]);
window.location.reload();

Since No_Parent_Account__c is a Boolean value, you don't need to surround it in quotes; this also makes it easier to read the code. Also note the use of the ! negation operator to remove that unnecessary if statement.

3
  • Thanks so much, that did the trick!! Much simpler code! Commented Dec 7, 2015 at 19:15
  • Sorry for hijacking this, but what would be a valid 1 click lightning experience alternative? salesforce.stackexchange.com/questions/159497/… Commented Feb 8, 2017 at 14:25
  • @ChristianSzandorKnapp It's okay, I don't mind. You've got a few different solutions to your question. See if any of those help you out.
    – sfdcfox
    Commented Feb 8, 2017 at 15:00
0

Change your if statement to:

if(a.No_Parent_Account__c == "true"){

And don't forget to retrieve the record first

var accts = sforce.connection.query("SELECT No_Parent_Account__c FROM Account WHERE Id = '{!Account.Id}'");

records = accts.getArray("records");

var a = records[0];

So, your complete sample would be

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var updateRecords = []; 
var a = new sforce.SObject("Account"); 

var accts = sforce.connection.query("SELECT No_Parent_Account__c FROM Account WHERE Id = '{!Account.Id}'");

records = accts.getArray("records");

var a = records[0];

if(a.No_Parent_Account__c == "true"){
a.No_Parent_Account__c = "false";
}else{
a.No_Parent_Account__c = "true";
}
newRecords.push(a); 
result = sforce.connection.update(updateRecords); 
window.location.reload();
7
  • Thanks. I made that change, but the behavior is the same, it's still ignoring the "else" portion. Commented Dec 7, 2015 at 18:58
  • Strange, hard to tell without looking at your data. Try doing a "console.log(a.No_Parent_Account__c)" or an "alert(a.No_Parent_Account__c)" before the if statement so you can verify that your comparing against the right value Commented Dec 7, 2015 at 19:00
  • I tried both. The "alert" returns an error box that just says Undefined. The console.log just mimics the same behavior of ignoring the else. Commented Dec 7, 2015 at 19:05
  • that explains. you're not retrieving the value from the DB before evaluating. Not sure really how to do that. Will research and updte my post. UPDATE: Done, with new code Commented Dec 7, 2015 at 19:06
  • @SebastianKessel There's really no need to do all that work in a custom button.
    – sfdcfox
    Commented Dec 7, 2015 at 19:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .