Thursday, October 20, 2016

How to choose between Trigger and Lightning Process Builder


With the introduction of Lightning Process Builder, we can do many things which were kind of limitations of Workflows. Salesforce introduces Lightning Process Builder as successor of Workflow rules, thus allowing more config options through click and reducing amount of apex customization required. Process Builder supports all the features from Workflows with the exception of sending outbound message without apex code. I will not be surprised if Salesforce decides to decommission Workflow in near future.

Previously there were few scenarios (listed below) which were not possible through workflow and we need to write Apex trigger to achieve those. Here are few of them -

  • If we need to perform complex validation
  • Before/After logic for record insert/update/delete/undelete operation.
  • Update/insert any objects or related records.
Problem with workflow is we can't update child records with workflow. Workflow supports only updation of parent record. So earlier we need to go for Trigger. But with the introduction of Process Builder, now we can update child records as well. Below is one case study -

Case Study:
It's a library management system - Universal Library. Library is having many books. If the book is not available, user will send the request for the book. The moment book is available, all the request status will change from 'Hold' to 'Ready to Pickup'. To achieve this below is the object model -
Here Book and Book Request are connected through Master-Detail relationship. Previously to achieve this requirement, we need to write Apex classes. But now we can do the same with Process Builder. Below is the screenshot of Process Builder -

With Process Builder you can add conditions to update selected records. For example, if library is having membership types - Gold, Silver, Broze, once a book is available, update only those requests which came from Gold membership holder and make them "ready to pickup".

There are few situations(sending outbound message) which can't be handled through config in Process Builder. For that, we need to write Apex class/method. We need to make sure your method is annonated with @InvocableMethod so that it can be used in Process Builder. With Invocable methods, there are few limitations, which we need to keep in mind. Limitations can be found @ https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

Excited, right!! If you want to convert existing Apex Trigger to Process Builder, please keep in mind the below points -
  •  You need to make sure that in your trigger any field update is not used by subsequent trigger or workflow. The reason behind is that execution order wise Process Builder will execute after workflow rules.
  • With enabling Recursion option in Process Builder, one record can be reevaluated maximum 5 times. So you need to make sure you are not disturbing Salesforce limits here.
Scenarios where Trigger is the best approach:
There are still scenarios where Apex Trigger is the best approach. I am trying to write down few of them. If you have anything in your mind or faced any issues during your development for which you have moved to Apex Trigger, please feel free to let me know. I will be more than happy to include those scenarios.
  • Complex Validation Rules in not possible with Process Builder. You need to got for Apex trigger to achieve that. Here is the Salesforce Idea to add Validation rule capability in Process Builder - Salesforce Idea.
  • Apex Trigger's context - Delete/Undelete with before/after even is not possible with Process Builder.
I will really appreciate if you provide your feedback. Thanks.

1 comment:

  1. I can add that Process builder cannot be used for Bulk execution ;)
    You'll have errors

    ReplyDelete