Wednesday, November 28, 2018

Call Apex Code from a Process builder in Salesforce

Today in this topic Call Apex Code from a Process builder in Salesforce we will see how we can invoke apex class from Process builder.

To call an apex method from process builder we need to define the method with @InvocableMethod 
in apex class.When a method is define with the mentioned annotation we can call this method from process builder using action type as Apex.

SCENARIO:

I am having a scenario where when on parent object if the status is set to CLOSED, I need to delete all related records.

Here I am taking Parent object as Testobject1__c and Child object as Testobject2__c.

Let's start:

Go to Setup > Process builder > click new.

Enter the process name, description, select "The process starts when" as  A record changes.

Add the object (Testobject1__c ), select when a record is created or edited. Enter the criterion
as shown in image below to specify condition Status='CLOSED'.


Call Apex Code from a Process builder in Salesforce.



under IMMEDIATE ACTIONS: select the action type as apex,mentioned the apex class,  set the apex class variable as shown in image below,

NOTE:  Field ObjId is the list of Id I am passing to apex class method.

Calling apex method from process builder in Salesforce


Now my Apex class is,


public class DeleteChildRecords{

@InvocableMethod
public static void method1(list <id> objId )
{
list <Testobject2__c> list1= [select id from Testobject2__c where Testobject1__c in:objId ];
delete list1;
}


}

No comments:

Post a Comment