1

I have a custom button which checks for the following condition but I'm getting an error like "Invalid left-hand side assignment :

Display Type: Detail Page Button

Behavior: Execute Javascript

Content Source: Onclick Javascript

Code:

if( 
({!Lead.Content_Approval__c} == True && 
{!Lead.Relations_Approval__c} == False &&
{!Lead.Final_Approval__c} == False &&
{!Lead.Approval_Confirmation__c} == True && 
{!Lead.Relations_Approval_Confirmation__c} == False &&
{!Lead.Approved__c} == False) 

){ 
    if (confirm('Are you sure you want to log this activity?')) { 
    // Fire away! 
    window.location.href = "/apex/sampleleadbatch?scontrolCaching=1&id={!Lead.Id}"; 
    } else { 
            // Go back to the Lead page 
        } 
    } 
else { 
alert("Please get approved the  Lead before hitting 'Send'."); 
}
3
  • Are you encountering this when you're trying to compile your code?
    – sfdx
    Mar 1, 2016 at 19:51
  • Are you using Salesforce Classic or Lightning? Mar 1, 2016 at 19:52
  • salesforce classic Mar 1, 2016 at 19:54

1 Answer 1

6

You have a few problems with your code. Let's start with the first one: Boolean values are not capitalized in JavaScript, and JavaScript is case sensitive. That means you should use true and false instead of True and False.

Secondly, there's no reason you need to compare to true or false directly; simply use the value for true, or its inverted value for false. Third, you can combine all of your checks into a simple formula:

if({!Lead.Content_Approval__c && NOT Lead.Relations_Approval__c &&
 NOT Lead.Final_Approval__c && Lead.Approval_Confirmation__c &&
 NOT Lead.Relations_Approval_Confirmation__c && NOT Lead.Approved__c}) {
  if(confirm(...)) {
    // Do action
  } else {
    // Go back
  }
} else {
  alert(...);
}

Finally, you can write a slightly more optimized version by swapping around some Boolean logic:

if(
{!
AND(
  Lead.Content_Approval__c, Lead.Approval_Confirmation__c,
  NOT(OR(
      Lead.Relations_Approval__c, Lead.Final_Approval__c, Lead.Relations_Approval_Confirmation__c
    )))}) {
  if(confirm(...)) {
    // Do action
  } else {
    // Go back
  }
} else {
  alert(...);
}

You can do this because NOT x AND NOT y is the same as NOT(x OR y).

2
  • 1
    +1 for don't compare Booleans to Booleans.
    – Adrian Larson
    Mar 1, 2016 at 20:25
  • 2
    +1 for suggesting use of DeMorgan's Law to simplify an expression.
    – Derek F
    Mar 1, 2016 at 20:58

You must log in to answer this question.

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