Do you want to know how to call an apex class in Lightning Process Builder? Let’s discuss all you want to know shortly here in this article.

Overview of lightning process builder:

The Process Builder is a new workflow tool from spring release 15 that promises you to easily automate your business processes by providing a powerful and user-friendly visual representation of your process as you build it. The Process Builder’s simple and powerful design which not only creates, updates records, send emails, call a Flow, Post to Chatter and Submit for approval but also calls apex class in order to achieve requirements which can’t be done by any of the above.

To answer this question @InvocableMethod comes into play. A process builder can call only apex classes which implemented @InvocableMethod annotation for a static method in it.

Only one @InvocableMethod is allowed for one class.

At the time of writing this post, the current release is summer 15, until now the method accepts only one argument.

In order to pass more than one argument from process builder, you want to create a class with @InvocableVariable this allows you to pass multiple variables from process builder.

Requirement:

Delete the related contacts of an account when a checkbox is checked in an account.

Solution:

Create a process that executes when that checkbox value is true which calls an apex class to delete the related contacts.

* Don’t forget to Activate the process after you have created it.

The process builder will look like this

Create a class named as DeleteRelatedContacts and copy paste the below code.

public class DeleteRelatedContacts { //Method to delete contacts @InvocableMethod public static void deleteContacts(List<Id> accountIds) { List<Contact> conlist = [select id from contact where AccountId IN : accountIds]; //Delete if list is not empty if(conlist.size() > 0) { delete conlist; } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class DeleteRelatedContacts { //Method to delete contacts @ InvocableMethod public static void deleteContacts ( List < Id > accountIds ) { List < Contact > conlist = [ select id from contact where AccountId IN : accountIds ] ; //Delete if list is not empty if ( conlist . size ( ) > 0 ) { delete conlist ; } } }

conclusion :

Here we can select source object, criteria/event to initiate the process as well and the action to be taken on the occurrence of the event.

In actions section of process builder, we can execute apex method as well. This is the main advantage of process builder as we cannot execute the apex class using flows or workflows.

This is the main advantage of process builder as we cannot execute the apex class using flows or workflows.

Do you want to add something in this article? share your views in the comment section.