Tuesday 28 July 2015

Lightning Process Builder (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 invokable variable we can process  the business logic in invokable 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('requestObj.leadId@@:'+requestObj.leadId);          
                              System.debug('requestObj.firstname@@:'+requestObj.firstname);
                              System.debug('requestObj.lastname@@:'+requestObj.lastname);      
             
                      }
              //We can write our own business logic herer.
             
             
       }
      
      
}


Activating The process:


Lead Creation:

Output(Debug Log):

Refererences:

No comments:

Post a Comment

How to include a screen flow in a Lightning Web Component

 Hi, Assume  you have a flow called "Quick Contact Creation" and API Name for the same is "Quick_Contact_Creation". To i...