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:

  1. Create a record

  2. Email Alert

  3. Call any flow

  4. Post to chatter

  5. Quick Action

  6. Submit for Approval

  7. Update records (Not only parent but any related record of current record)

  8. Call Apex Class

Invoke Apex with Lightning Process Builder

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.

Invoke Apex with Lightning Process Builder
Invoke Apex with Lightning Process Builder

So you can see now it is quite easy to call apex method on criteria basis.

 

Things to consider

  1. We can’t update same record with apex call as we generally do in before insert/update trigger because values are read only.

  2. You will get exception email if there is any error.

  3. Only one method in a class can have the InvocableMethod annotation.

  4. 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.