Invoking apex class from process builder

Overview 

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 cant be done by any of the above.

What are the classes which can be called from the process builder ?

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. 

How many  @InvocableMethod a class can have?

Only one  @InvocableMethod is allowed for one class.

How many arguments does a  @InvocableMethod can receive ?

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

What if i want to pass multiple variables to apex class?

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

image

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

 

public class DeleteRelatedContacts {
  
    //Metod to delete contacts 
    @InvocableMethod
    public static void deleteContacts(List<Id> accountIds)
    {
        List<Contact> contactListToDelete = [select id from contact where AccountId IN : accountIds];      
 
        //Delete if list is not empty
        if(contactListToDelete.size() > 0){
            delete contactListToDelete;
        }        
    }    
    

}

 

Rating: 
No votes yet