2

I'm creating a custom execute JavaScript button for the Opportunity detail page and trying to use an IF statement to conditionally load a resource with REQUIRESCRIPT. The goal is to base the condition on the value of a checkbox (Active__c) on the related Account record. Here's the button formula:

{!
 IF(Account.Active__c,
  REQUIRESCRIPT("data:application/javascript;base64,YWxlcnQoJ2hlbGxvJyk="),
  ''
 )
}
alert('{!Account.Active__c}') 

The formula never evaluates to true regardless of the Active__c field being true or false so the REQUIRESCRIPT never loads. If I base the condition on a checkbox on the Opportunity record, it works fine. The click-activated alert displays the correct values for Active__c so the formula is getting something for that field, but it doesn't seem to be able to reference it correctly in the IF statement. Oddly, if I create some other type of condition like Account.Text_Field__c = 'text', it works correctly. The issue seems to be limited to booleans. Is this just a limitation of custom buttons, or am I doing something wrong here?

In this mockup, the JavaScript encoded in the REQUIRESCRIPT is just an alert popup that says 'Hello' when the script loads.

  • Did you try Active__c == 'true' ? ;-) – Cloud Ninja Feb 18 '14 at 5:57
  • 'true' as a string with quotes errors out because it only accepts a boolean value. I have tried Active__c == true with no success. Active__c != false and the !Active__c don't work either. – Scott McClung Feb 18 '14 at 22:10
1

I think the problem is that REQURIESCRIPT is handled by the server and is one of those few occasions where you can't use a merge field for the parameter. When you tested with a text field did you use == or = as in your example? In the case of the latter it would have done an assignment and fulfilled the condition regardless of the value in the field used.

I'd include the script at the top regardless, but put the code inside the script into a function. Then you can simply call that function or not based on the field's value.

{!REQUIRESCRIPT("data:application/javascript;base64,YWxlcnQoJ2hlbGxvJyk=")}

if({!Account.Active__c})
{
  myFuncInTheRequiredScript();
}

If you absolutely want to use the style you have, and there is a bug with booleans (seems doubtful, but you never know) then a cast might do the trick: {!IF((Account.Active__c & '') == 'true',...

  • The JavaScript in the REQUIRESCRIPT is intended to carry out a task when the page loads rather than when the button is clicked. I use this method to display a jquery pop up alert to the user when they open the opportunity record. Moving the logic into the button JavaScript like you demonstrate makes it dependant on the user clicking the button. (correct?) This is an example of what does work: {!IF( Account.AccountNumber == 'CD656092', REQUIRESCRIPT("data:application/javascript;base64,YWxlcnQoJ2hlbGxvJyk="),'')} alert('{!Account.AccountNumber}') – Scott McClung Feb 21 '14 at 3:04
0

Please try this code:

if({!Account.Active__c}){
    alert('It's alive!'); 
}
  • This didn't work either. – Scott McClung Feb 18 '14 at 23:19
  • It should not not work like this. I just gave you example without any alert inside. Please try it now. This If must work :) – Artur Kępczyński Feb 19 '14 at 5:11

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.