Salesforce recently released Lightning Process builder for their customers. It is a UI workflow tool that helps admin to automate business processes in powerful manner.
It is similar to Workflow but it also provides great flexibility with following action which is not available with Workflow
It provides following action:
- Create a record
-
Email Alert
-
Call any flow
-
Post to chatter
-
Quick Action
-
Submit for Approval
-
Update records (Not only parent but any related record of current record)
-
Call Apex Class
In this post we will cover calling apex class from Process builder.
To learn about the apex class invocation we will use new annotation method called @InvocableMethod which was introduced in Spring ’15 release. This annotation lets us mark an Apex method as being something that can be ‘invoked’, or called
In our example we are going to update parent account last quality check with current date while inserting new account record.
1. Create apex class and method
public class AccountInsertAction { @InvocableMethod(label='Insert Accounts' description='Inserts the accounts specified and returns the IDs of the new accounts.') public static void insertAccounts(List<Account> accounts) { for(Account acc : accounts) { acc.Last_Quality_Check__c=system.today(); } update accounts; } }
2. Create a new Process
After creating a new class we are now going to create a new process which will update last_quality_check__c field with current date as shown below of parent account.
So you can see now it is quite easy to call apex method on criteria basis.
Things to consider
- We can’t update same record with apex call as we generally do in before insert/update trigger because values are read only.
-
You will get exception email if there is any error.
-
Only one method in a class can have the InvocableMethod annotation.
-
Its a little less testable compared to trigger execution.
Obviously this example is very simple, but the Apex you use could be quite a bit more complicated from updating parent record, deleting task, deleting related records on update.
Comments