Hi and welcome to another very exciting tutorial! Today I’ll provide step-by-step instructions on how to invoke a trigger from a process builder in Salesforce. That way you’ll have more control over the code that is set in your org.
The main advantage of starting a trigger from a process builder is that you have the ability to turn off the automation whenever it’s necessary. If you were to hardcode the trigger criteria it’d take quite a while to deploy changeset to turn it off. Another cool thing is that you’ll have less code in your trigger. All triggering criteria will be specified in the process builder.
Step 1: Create APEX
Let’s create a very simple apex class. In order to access apex class functions from process builder, you need to use @InvocableMethod annotation. Please take a moment to read about this annotation in the Developer Guide to get familiar.
As you see, the apex class above does not do anything in particular. It’s more like a template for your future triggers. Essentially this class gets a list of records from process builder so that you can use them in apex.
Label parameter helps to identify apex class function from Process builder
Description provides some details about the trigger.
Step 2: Create a Process Builder With Triggering Criteria
For this example, I’ll create some simple process builder on a ‘Contact’ object. Go to Setup -> Type in the search bar ‘Process Builder’. You should be able to see filtered results shortly. If you are not familiar with Process Builder – please take this awesome trailhead to get familiar.
Click ‘New‘ to create a new process.
Process Name: Contact Trigger.
API Name: Will be auto-populated.
Description: Execute Custom Logic For Contact.
The process starts when: A record changes.
Next, go ahead and add a ‘Contact’ object. You can do it by clicking ‘Add Object‘ and finding Contact in the list.
Set ‘Start the process‘ when a record is created or edited.
Obviously, you could use the other option. It all depends on your use case. I prefer to use ‘when a record is created or edited’ just because it eliminates the need of creating another process builder for worklows that are supposed to fire on updates. If you need process builder to fire on insert, you could use ISNEW() formula to check if record is inserted.
Let’s add our first action. For this example, I’ll use very simple trigger criteria. I want our process builder to fire only when a phone number is populated on a contact record.
Criteria Name: Phone is not Empty.
Criteria for Executing Actions: Conditions are met.
Set conditions: [Contact].Phone – Does Not Equal – Global Constant – $GlobalConstant.EmptyString
Conditions: All conditions are met (AND)
Important! Make sure to click the ‘Advanced’ link to set checkbox to ‘Yes’. That way your action will fire only when specified changes are made to the record. In other words, it will only trigger when a phone field changes from blank to some value.
Otherwise, action will fire every time when a contact record is updated while the phone number is not blank.
Once done, don’t forget to click ‘Save’. Next, let’s add action by clicking ‘Add Action’ link.
For Action Type select ‘Apex’
Once you select ‘Apex’ from the list it’ll display some options.
Action Name: Execute Trigger
Apex Class: Start typing Custom… It’ll then display full class name ‘Custom Logic on Contact’ This title comes from a label attribute in our apex class example.
Lets set variables. Since our apex class function accepts a list of contacts that have a name ‘contacts’, the field variable will be ‘contacts’.
Type: Field Reference
Value: Contact Record that Started Your Process
Your apex action should look like this: Again, please make sure to hit ‘Save’ at the end. It’s so annoying to re-do the same thing over again.
Alright, once the action is saved the very last step would be to click ‘Activate’ to make your trigger live.
Now you should be able to easily move your hardcoded triggers into process builders. While there are obvious advantages of using such approach, there are still some things to take into consideration:
- Triggers will only fire on after insert/update. You can’t use a process builder for all before & delete/undelete operations.
- If you have multiple triggers under the same object, you can’t turn some of them off temporarily. You either have to make a copy of the entire process or isolate each trigger into a separate process builder. I don’t recommend having a process builder per trigger.
These are all precautions. Other than that I hope you’ve learned something new today & can apply this knowledge in your projects. Please let me know if I’m missing something, or if you have questions. See ya’ll next time!