Calling a Workflow from a Custom Button

I had a requirement to place a custom button on a layout that needed to call a workflow to accomplish some action.  This can’t be done natively but there is a neat little trick you can employ.

Workflows are triggered when specific field criteria is met.  With this in mind, we need to create a Javascript button that updates a checkbox field on the object.  I created a ‘toggle’ checkbox and set its default to unchecked.

I created a button to execute Javascript.  The code is straightforward: grab the object, check the checkbox, and update the object.  Some error handling will inform the user if the operation was successful or not.


var myquery = "SELECT Id, Generate_Preview_Toggle__c FROM Special_Waste_Profile__c WHERE Id = '{! Special_Waste_Profile__c.Id}' limit 1"; 

result = sforce.connection.query(myquery); 
records = result.getArray("records"); 

var mySWP = records[0]; 
var updateSWP = new Array(); 

mySWP.Generate_Preview_Toggle__c = true; 

result = sforce.connection.update(updateSWP); 

alert('Check your email, your preview is on its way!'); 
window.location = "/" + "{!Special_Waste_Profile__c.Id}"; 
alert('Contact support. Could not update: '+result); 

The next step is to create a workflow rule that acts when my  Generate Preview Toggle field is checked.  The workflow takes care of the task I need to accomplish, and then returns the toggle checkbox to unchecked. This is important, because otherwise the workflow cannot be initiated a second time from the button.

Remember to activate your workflow!

Author: Matt

Matthew is a highly accomplished and award-winning programmer who is well versed in a variety of the hottest technologies powering today's most successful companies. With 11+ years of experience, Matthew has done it all: from the most rudimentary tasks to complex implementations of entire applications, CRMs, and pieces thereof. Today he spends his time mastering administration, development, and implementation as he mediates between stakeholders and project owners to successfully interface a complex company in a complex industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.