How To Dip Your Toe Into Javascript

If you are staying with Classic, JavaScript is a great tool, but getting started can be intimidating. At least for me it was. You build a button, lets you save it, and then, you get an obscure ‘undefined token’ error.

I’ve found the best way to start learning is to start simple. Create a button to do one action like update a checkbox on the record.


  • Create a checkbox field (‘Activate_Button_Checkbox__c’)

  • Get your object name (‘Account’).

  • Substitute your checkbox field name and object name into the code below.

{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")} var newRecords = [] var A = new sforce.SObject("Account"); a.Id = "{!Account.Id}"; a.Activate_Button_Checkbox__c = 1; newRecords.push(a) result=sforce.connection.update(newRecords); window.parent.location.href="{!Account.Id}"

Clickpath: Salesforce Setup/Accounts(Object)/Buttons, Links & Actions/New

*Make sure Behavior is ‘Execute JavaScript’

Home Free!

Once you’ve tested that your checkbox gets checked, the world is your oyster. You can now build declarative automation (i.e. workflows, process builders/flows) by using ‘Activate_Button_Checkbox__c=TRUE’ in your entry criteria.

The trick is, to be able to reuse the button, you’ll need to uncheck the checkbox as part of your automation. (A field update where ‘Activate_Button_Checkbox__c=FALSE’.

The benefit of setting up your button this way is that if your process changes, your changes will be made in your declarative automation instead of in JavaScript.

Recent Posts