Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I created a button which will update SAPReleased__c only if Approved__c is true(SAPReleased__c and Approved__c are both check boxes). What I don't understand is.. Why is this working!??

        alert(sampling.Approved__c); 
        if(sampling.Approved__c == 'true'){ 
        alert(sampling.Approved__c);
        alert('Inside condition');  
        sampling.SAPReleased__c = true; 

        }else if (sampling.Approved__c == 'false'){ 
        alert('Sample must be approved before it can be booked.'); 
        }
        var result = sforce.connection.update([sampling]); 
        window.location.reload();

Now look at my conditions

if(sampling.Approved__c == 'true')
else if (sampling.Approved__c == 'false')

This doesn't make any sense to me? I am checking to see if they are 'true' or 'false', but isn't this checking to see if its a String of true or false while it should be expecting a boolean?

Is this an expected outcome or is this something cooky?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

This is working because Javascript is doing the type conversion for you. You need to use a '===' to strictly evaluate a boolean like true or false. With a '==' as I understand it, type conversion has to occur before the comparison can be evaluated and type conversion can lead to strange results. In this case though, it's leading to your intended result, which is conversion from boolean to string and the correct evaluation.

Here is a pretty good blog post explaining different equals operators in Javascript: Why Use the Triple-Equals Operator in JavaScript?

share|improve this answer
1  
It is so I will leave alone, it's just very interesting.. –  EricSSH Apr 14 at 22:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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