+ Start a Discussion
SandrineSandrine 

Execute a processbuilder when a button is clicked

Hello,

I want to execute a processbuilder when a button is clicked, 
my custom button is like below
User-added image

thanks you for suggestion
Best Answer chosen by Sandrine
RKSalesforceRKSalesforce
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. You can put your condition as well.
Use below code in your button.
//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!

Please mark as a best answer if helped.

All Answers

RKSalesforceRKSalesforce
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. You can put your condition as well.
Use below code in your button.
//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!

Please mark as a best answer if helped.
This was selected as the best answer
SandrineSandrine
PErfect solution !
JP LeonardJP Leonard
https://medium.com/@walters954/triggering-process-builder-from-button-misadventures-of-a-salesforce-intern-bfb3db608773
milesinsidemilesinside
honda57honda57
I thought Javascript buttons were not supported in Lightning?
Mark MogridgeMark Mogridge

Well done Ramakant! Brilliant.

 

One twist on this approach (a bit clunky but 'native') is to have a Button > that uses a VisualForce page > to call a Flow that changes a field value > that fires the Process!

This has the benefit of avoiding the JavaScript button issue.

 

First create a VisualForce Page that calls the Flow

<apex:page standardController="Customer_Card_Program__c" >

/* 1. Set the interview name to the API of the Flow + and give the ID / URL of the record */
    <flow:interview name="Customer_Card_Program_Run_Promote_Demote_Flow" finishLocation="/{!Customer_Card_Program__c.Id}">

/* 2. Set your Parameter for the Flow */
        <apex:param name="vCustomerCardProgramID" value="{!Customer_Card_Program__c.Id}" />
        <apex:param name="vRunMode" value="Draft" />

    </flow:interview>
</apex:page>
 

Then create a Button that uses the content of the VisualForce page 

User-added image

Darren  FerneyhoughDarren Ferneyhough
I agree with JP Leonard and Joel Magnusson that it does appear that Ramakant appears to be passing off someone else's work here as his own and then specifically asking to be marked as best answer seems a bit off

Proper credit is due to the original author of this solution Warren Walters https://medium.com/@walters954/triggering-process-builder-from-button-misadventures-of-a-salesforce-intern-bfb3db608773