Getting Started with Process Builder – Part 33 (Call Apex Class from a Process )

Getting Started with Process Builder – Part 33 (Call Apex Class from a Process )

Last Updated on November 23, 2020 by Rakesh Gupta

Big Idea or Enduring Question:

How can you automatically delete unaccepted Quotes using Process Builder?

Objectives:

After reading this blog post, the reader will be able to:

  • Understand @InvocableMethod Annotation
  • How to call an Apex method using Process Builder

Business Use case

Steven Greene is working as a System administrator in Universal Container. He has received a requirement from the management, whenever a quote is accepted by the customer, then auto-delete related quotes from the same Opportunity to save the data storage.

Automation Champion Approach (I-do):

The new Lightning Process Builder is an enhanced version of Workflow Rule, that helps you easily automate your business processes by providing a powerful and user-friendly visual representation of your process as you build it. With the Process Builder, you can perform the following actions

  • Create a record
  • Update any related record
  • Quick action
  • Launch a flow
  • Send an email
  • Post to Chatter
  • Submit a record for approval
  • Invoke a Process from Another Process
  • Work with Quip
  • Send a Custom Notification
  • Send a Survey Invitation
  • Call Apex Code
  • Work with Salesforce Anywhere (Beta)

While the Process builder is very flexible out of the box, there are a few business use cases that are not achievable using it. For example

  • Process Builder doesn’t support outbound messages
  • Process Builder doesn’t allow us to delete a record (You can now use Lightning Flow – I am going to write a separate article on it)

While this can be solved using various automation tools like Apex or Flow, we will use Process Builder and call an Apex method. To call an Apex method, add the Call Apex action to your process and select an Apex class with a @invocable method Annotation. It means they allow us to extend the Process Builder by writing Apex code that meets certain criteria and then invoking the Apex from our Processes. If the class contains one or more invocable variables, manually enter values or reference field values from a related record. @InvocableMethod Annotation supports bulk operations. Lets begin the solution for the business use case.

Before discussing the solution, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 5 steps to solve Steven’s business requirement using Process Builder. We must: 

  1. Create an Apex class
  2. Define process properties
  3. Define evaluation criteria
  4. Define process criteria 
  5. Add action – call an Apex class

Step 1: Create an Apex class and Test class   

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Clicks on the New button.
  4. Copy the code below and paste it into your Apex Class. 
  5. Click Save.

public class DeleteUnacceptedQuotes
{
    @InvocableMethod
    public static void QuoteDelete(List<Id> OpportunityIds)
    {
        List<Quote> Quotes =[select id from quote
                          where Opportunity.id in :OpportunityIds
                          and Status != 'Accepted'];
 
        delete Quotes;
    }
}

Step 2: Define Process Properties

  1. Click Setup.
  2. In the Quick Find box, type Process Builder.
  3. Select Process Builder, then click New.
  4. Name the Process and click the Tab button. The API Name will populate. 
  5. As a best practice, always input a description
  6. The process starts when A record changes.
  7. Click Save.

Step 3: Define Evaluation Criteria

  1. Click on the Add Object node to begin selecting the evaluation criteria.
  2. Select the Quote object from the dropdown list.
  3. Start the process when a record is created or edited.
  4. Click Save.

Step 4: Define Process Criteria

  1. Click the Add Criteria node to begin defining the process criteria.
  2. Name the criteria.
  3. The criteria should execute actions when the conditions are met.
  4. Set Conditions
    1. Row 1
      1. Field: Quote | Status
      2. Operator: Equals
      3. Type: Picklist
      4. Value: Accepted
  5. Select All of the conditions are met (AND)
  6. Click Advanced
  7. Select Yes to execute the actions only when specified changes are made to the record.
  8. Click Save.

The reason why we would select the Yes checkbox for the question — Do you want to execute the actions only when specified changes are made to the record? — is to allow the Process Builder to execute the actions only if the record meets the criteria now, but the values that the record had immediately before it was saved didn’t meet the criteria. This means that these actions won’t be executed when irrelevant changes are made.

Step 5: Add Action – Call an Apex Class 

  1. Below Immediate Actions, click Add Action.
  2. For Action Type, select Apex
  3. Name the action.
  4. Select Apex class – DeleteUnacceptedQuote.
    1. Set Apex variables: 
      1. Row 1
        1. Field: OpportunityIds
        2. Type: Field Reference
        3. Value: Quote | opportunityId
  5. Click Save.

Almost there! Once everything looks good, click the Activate button. 

Proof of Concept

Now onwards, if a business user updates the Quote Status to Accepted, Process Builder will automatically delete all other Quotes (related to the same Opportunity) that do not have the status as Accepted.

  1. Currently, there are three Quotes attached to the Opportunity West Mountain Sign as shown in the following screenshot:
  2. Now we update the Status on Quote 1_3 to Accepted and click Mark as Current Status
  3. Navigate back to the Opportunity to check out the Quote related list available on the Opportunity page. The other quotes will have been deleted as shown in the following screenshot:

Formative Assessment:

I want to hear from you!  

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? 

Let me know by Tweeting me on @automationchamp, Call Apex Class from a Process

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

 

3 thoughts on “Getting Started with Process Builder – Part 33 (Call Apex Class from a Process )

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.