Calling Apex From Process Builder

The Process Builder is a workflow tool that helps you easily automate your business processes by providing a powerful and user-friendly graphical representation of your process as you build it. The Process Builder’s simple and powerful design allows you to:§  Create your processes using a convenient layout with point-and-click efficiency.

Create your whole process in one place rather than using multiple workflow rules.§  Create processes by collaborating with different teams in your business.§  Stop using Apex code to automate simple tasks.


I am trying to explain how we can invoke apex through process builder.To invoke apex from process builder ,we have to use annotations “invocableMethod” for method and “invocableVariable” for variable.We can invoke the apex class  which includes the invocablemethod only from the process builder.Navigation for Process Builder:


Setup- >Build -> Workflow & Approvals –> Process Builder

Creating Process:

Elements of Process:

Adding Salesforce object:

Adding Conditions:

Apex Invocation: (Apex class which contains Invocable methods only appear in the Apex Action of process builder)

Here apex variables are invocable variable which are useful to hold the values of record like lead id ,firstname,LastName as shown above. By using the apex invocable variable we can process  the business logic in invocable method as shown below.

Example Class:

globalclass InvocableMethodcls {

    //An invocable variable used as input or output variables in the process builder
    globalclass ActionRequest {
        @InvocableVariable(required = true)
        public ID leadId;

        @InvocableVariable
        public String phone;

        @InvocableVariable
        public String email;

        @InvocableVariable
        publicLead leadObj;

        @InvocableVariable
        public String firstName;

        @InvocableVariable
        public String lastName;
    }
    //This invocable method is used for processing the business by taking the input from process builder
    @InvocableMethod(label = 'Invoke Business Logic')
    globalstaticvoid invokeService(List < ActionRequest > requests) {
        for (ActionRequest requestObj: requests) {
            //Accessing the values from process builder when record is inserted
            System.debug('[email protected]@:' + requestObj.leadId);
            System.debug('[email protected]@:' + requestObj.firstname);
            System.debug('[email protected]@:' + requestObj.lastname);
        }
        //We can write our own business logic herer.
    }
}

Activating The process:

Lead Creation:

Output(Debug Log):

References:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htmhttps://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableVariable.htmhttps://help.salesforce.com/HTViewHelpDoc?id=process_overview.htm

Leave a Comment