Hands on 4 – calling an Apex class from Process Builder

Process Builder allows you to call an Apex class that includes methods annotated with @InvocableMethod. By calling an Apex class from Process Builder, you can add customized functionalities, such as auto-converting leads, deleting records, or running lead assignment rules. You can also pass a required value into Apex class variables.

When no other process actions can get your job done, by calling an Apex method, you can add customized functionality for your users. You can only call an Apex class from Process Builder or Visual Workflow that have the @InvocableMethod annotation. This means that it is possible to extend the Process Builder functionality by writing an Apex class that executes your business logic, and then invoking the Apex from your process. If the class contains one or more invocable variables, then you have to manually enter values or reference field values from a related record.

Let's look at a business scenario. Suppose that Alice Atwood is working as a system administrator at Universal Containers. She has received a requirement from the management to auto-delete open cases, if the out of business checkbox is checked on the account record.

The following is the approach that we are going to use in order to solve the preceding business requirement:

  1. First of all, create a custom out of business checkbox field on the account object, and make sure that you set the field-level security for the respective profiles.
    1. The next step is to write an Apex class that will delete open cases for accounts whose IDs we are going to pass through the process. To create an Apex class, navigate to Setup (gear icon) | Setup | PLATFORM TOOLS | Custom CodeApex Classes, and then click on the New button. The following is sample code; AccountIds is the record ID of accounts where out of business is updated as true:
            public class DeleteOpenCases
    {
    @InvocableMethod
    public static void CaseDelete(List<Id> AccountIds)
    {
    List<Case> Cases =[select id from case
    where Account.id in :AccountIds
    and Status != 'Closed'];
    delete Cases;
    }
    }
    1. The next step is to create a process to call the Apex class that you have just created, only when the out of business checkbox is checked on the account record. To create a process, navigate to Setup (gear icon) | Setup | PLATFORM TOOLS | Process Automation | Process Builder, click on the New button, and enter the following details:
      • Name: Enter the name of the process - Delete open cases - Out of business accounts.
      • API Name: This will be auto-populated, based on the name.
      • Description: Write some meaningful text, so that other developers or administrators can easily understand why this process has been created.
      • This process starts when: Configure the process to start when a record is created or edited. In this case, select A record changes.
    2. Once you are done, click on the Save button.
    3. After Define Process Properties, the next task is to select the object upon which you want to create a process and define the Evaluation Criteria. For this, click on the Add Object node. It will open an additional window on the right-hand side of the process canvas screen, where you will have to enter the following details:
      • Object: Start typing, and then select the Account object.
      • Start the process: For Start the process, select when a record is created or editedThis means that the process will fire whenever a record gets created or edited.
      • Recursion - Allow process to evaluate a record multiple times in a single transaction?: Select this checkbox only when you want the process to evaluate the same record up to five times in a single transaction. In this case, leave this box unchecked.

    The preceding steps will look like the following screenshot:

    1. Once you are done, click on the Save button.
    2. After defining the Evaluation Criteria, the next step is to add the Process Criteria. Once the process criteria are true, the process will execute the associated actions. To define the process criteria, click on the Add Criteria node. It will open an additional window on the right-hand side of the process canvas screen, where you will have to enter the following details:
      • Criteria Name: Enter a name for the criteria node. Enter Only for out of business accounts as the criteria name, in this case.
      • Criteria for Executing Actions: Select the type of criteria that you want to define. You can select either Formula evaluates to true, or Conditions are met (a filter to define the process criteria), or No criteria-just execute the actions!. In this case, select Conditions are met.
      • Set Conditions: This field lets you specify which combination of the filter conditions must be true for the process to execute the associated actions. In this case, set 
        [Account].RakeshGupta__out_of_business__c to True.
      • Conditions: In the Conditions section, select All of the conditions are met (AND). This field lets you specify which combination of the filter conditions must be true for the process to execute the associated actions.
      • Under Advanced, select Yes to execute the actions only when the specified changes are made.

    The additional window will look as follows:

    1. Once you are done, click on the Save button.
    2. Once you are done with the process criteria node, the next step is to add an immediate action to delete open cases when the account is out of business. For this, we will use the Apex action, available in Process Builder. Click on Add Action, available under IMMEDIATE ACTIONS; it will open an additional window on the right-hand side of the process canvas screen, where you will have to enter the following details:
      • Action Type: Select the type of action; in this case, select Apex.
      • Action Name: Enter a name for this action - Delete open cases.
      • Apex Class: Select the Apex class that you want to execute; in this case, select RakeshGupta__DeleteOpenCases.
      • Set Apex Variables: Use this to set values for sObject variables and sObject list values. For the current use case, map the AccountIds field to [Account].Id.

    The window will look as follows:

    To assign values to multiple variables, click on the Add Row link.

    1. Once you are done, click on the Save button.
    2. The final step is to activate the process. Click on the Activate button on the button bar. Finally, the process will appear:
      1. Go ahead and identify an account where there are a few open cases, as shown in the following screenshot:
      1. Now, update the out of business checkbox to True, and then reload the page. It will look like the following screenshot:

      Before testing the process, make sure that you have activated it.

      ..................Content has been hidden....................

      You can't read the all page of ebook, please click here login for view all page.
      Reset
      18.222.197.35