Triggering Process Builder From Button — Misadventures of a Salesforce intern

I work as a Salesforce developer/admin at a medium size org. Every so often my bosses ask me to create something “Weird” on Salesforce. They come up with the ideas and it is my job to bring it to life. This type of dynamic is really fun for learning new stuff but does not always lead to best practices.

How to trigger a process builder from a button press on an object?

My example will go over how to create a custom button on the account that will increase a number field which will cause a process builder to fire.

Requirements: A new number field, custom button, process builder. Some understanding of JavaScript and SOQL.

Create a new number field. (I named mine Process Builder Trigger) Name it and remember to give it a description so later admins/devs do not get confused.

Create a new custom button. Label it and description.

Important: Display Type must be Detail Page Button. Behavior must be Execute JavaScript.

Put this code into your button. Changing the variable names as needed.

//I am assuming this code allows for the JS SOQL calls
//Check the resources section for SF documentation
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
//Select the current account records and put it into the record //variable
var records = sforce.connection.query("SELECT Id, Process_Builder_Trigger__c FROM Account Where id = '{!Account.Id}'");
//The records object come with other garbage so we need to only pull //out the account we are working with
//Pull the account object out of the records variable and set it to //accountRec
var accountRec = records.getArray('records')[0];
//Increase accountRec Process_Builder_Trigger__c 
//JS sees the number field as a string so this will make sure that //is a number - Number(accountRec.Process_Builder_Trigger__c)
accountRec.Process_Builder_Trigger__c = Number(accountRec.Process_Builder_Trigger__c) + 1;
//Update accountRec object 
sforce.connection.update([accountRec]);
//Reload the page 
window.location.reload();

Add the button to the object layout.

Last create a process builder that fires when the number field is changed. Now the process builder can do whatever you want!

Its really only 5 lines of code. The understanding that the button press will increase the number field amount instead of having to do an inline edit or going to the edit page is the key. In resources section I will include a gist of the current version that I have with user validation and the confirm dialog box to prompt the user.

But why?

One of the main reasons I went with the button and number field method instead of say a checkbox is because you can reuse the number field and it basically automatically counts the number of times the button has been pressed.

And because it was interesting.

Problems

One of the big issues I have found with using this method for firing the process builder is that it is slow. There is a delay from the time you click the button and when the page refreshes. For my org this is not an issue but for others it might be. Since there is a lag between the page refreshing and the button click. Some click happy users may be able to fire the process builder many times. This is also why I added in the JS confirm code — refer to the gist.

Disclaimer/Resources

I am not an “expert” at Salesforce administration or development. Currently (8/24/2016) I have 1 year experience with Salesforce. Working on my Salesforce Certified Platform Developer I. Of course there are other ways to solve this problem but this is the one that I went with to learn something new and basically see if it was possible.

Force.com Workbook documentation on JS and custom button.

Thanks for getting this far.