I am trying to do the same thing as this question (set the color of a custom detail page button):

Changing the color of a custom button

but I want the set the value based on the value of a field in record.

IF value is false, set button to red, else set button to green. Not sure the proper way to use IF statements with REQUIRESCRIPT (or if this is even possible....)

I tried the following code and it does not work as expected:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 

// identify the record 
var c = new sforce.SObject("Case"); 
c.id = "{!Case.Id}"; 

IF (!c.test-ajc__c, '{!REQUIRESCRIPT("data:application/javascript;base64,KGZ1bmN0aW9uKCkgewogIHZhciBidXR0b24gPSBkb2N1bWVudC5nZXRFbGVtZW50c0J5TmFtZSgndGVzdGFqYycpWzBdOwogIGJ1dHRvbi5zdHlsZS5iYWNrZ3JvdW5kID0gJyNGRTJFMkUnOwp9KCkpOw==")}', {!REQUIRESCRIPT("data:application/javascript;base64,KGZ1bmN0aW9uKCkgewogIHZhciBidXR0b24gPSBkb2N1bWVudC5nZXRFbGVtZW50c0J5TmFtZSgndGVzdGFqYycpWzBdOwogIGJ1dHRvbi5zdHlsZS5iYWNrZ3JvdW5kID0gJyMwMURGMDEnOwp9KCkpOw==")}')

// make the field change 
c.test-ajc__c = "TRUE"; 

// save the change 
sforce.connection.update([c]); 

//refresh the page 
window.location.reload();

Any suggestions on how I might get this to work?

Adding the non-encoded code:

(function() {
  var button = document.getElementsByName('testajc')[0];
  button.style.background = '#01DF01';
}());

The other encoding is the same code as above, just with a different color.

Either of the encodings work fine if I take out the IF statement and just put the REQUIRESCRIPT with the encoding at the top of the code (removing the basic REQUIRESCRIPT at the top).

With the IF statement in place, the button is green regardless of the value of the test-ajc field. Also, clicking on the button caused the following error to occur:

A problem with the OnClick JavaScript for this button or link was encountered:

Unexpected token ILLEGAL

  • 1
    The concept is right. But we can't read your encoded js code - it's for machines, not humans... what scripts do yo have encoded? Any errors in the browser console? Also you may use external script-files which need not to be encoded and will be much easier to debug. Finally after it works you can then encode your code as a last step. – Uwe Heim Aug 25 '14 at 17:11
up vote 3 down vote accepted

First off, interesting use of REQUIRESCRIPT and base64 encoding! Here's a couple of things to note:

Anything inside bang-brackets is evaluated at page load time, everything else is only evaluated once your button is clicked. Here's some code I cooked up to disable a button named "test button" if the complaint field is set to true on my case.

{!IF(Case.Complaint__c = false, REQUIRESCRIPT("data:application/javascript;base64,Y29uc29sZS5sb2coJ3N1Y2Nlc3NmdWwgbG9hZCcpOw0KdmFyIGVsbXMgPSBkb2N1bWVudC5nZXRFbGVtZW50c0J5TmFtZSgndGVzdGJ1dHRvbicpOw0KZWxtc1swXS5kaXNhYmxlZCA9IHRydWU7DQplbG1zWzBdLmNsYXNzTmFtZSA9ICdidG5EaXNhYmxlZCc7"),"")}

Just make sure you get your full statement for setting the color of your button inside of bang-brackets and it'll run at load time.

  • I tried your solution - but now I get an error "Invalid left side assignment" I have tried every combo of the IF conditional that I could think of: ' {!IF (Case.test_ajc__c, {!IF (!Case.test_ajc__c, {!IF (Case.test_ajc__c = false, {!IF (Case.test_ajc__c == false, ' All give the same error regarding invalid left assignment. – Aaron C. Aug 25 '14 at 18:46
  • Ok - this was the solution. The errors I was getting was due to using a hyphen where I should have been using an underscore when I was setting the value of the field once the button was clicked. Thanks for the help! – Aaron C. Aug 25 '14 at 20:05

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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