Getting Started with Salesforce Flow – Part 77 (Running Lead Assignment Rules From Salesforce Flow)

Getting Started with Salesforce Flow – Part 77 (Running Lead Assignment Rules From Salesforce Flow)

Last Updated on November 1, 2021 by Rakesh Gupta

To understand how to solve the same business use case using Process Builder. Check out this article Getting Started with Process Builder – Part 49 (Running Lead Assignment Rules From Process Builder).

Big Idea or Enduring Question:

How do you run the lead assignment rule from the Salesforce flow? Lead assignment rules allow us to automatically assign Leads to the appropriate queue or user. A Lead assignment rule consists of multiple rule entries that define the conditions and order for assigning cases. From a Salesforce User interface, a user can trigger assignment rules by simply checking the Assign using the active assignment rules checkbox under the optional section.

The problem arises when you need to insert or update the Leads from Salesforce Flow and wants to trigger assignment rules. Using the Salesforce Flow a Lead will be inserted or updated but the assignment rule will not be triggered as there is no check box to use the organization’s assignment rule or a prompt to assign using the active assignment rule.

Let’s start with a business use case.

Objectives:

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

  • Running the lead assignment rules from Salesforce Flow
  • Understand @InvocableMethod Annotation
  • How to call an Apex method using Salesforce Flow

Business Use Case

Pamela Kline is working as a System administrator at Universal Containers (UC). She has received a requirement from the management to update the following Lead fields when Lead Source changed to Partner Referral.

  • Status = Working – Contacted
  • Rating = Hot

As data changed by the process, she wants to fire the assignment rule as soon as the process updates the lead record.

Automation Champion Approach (I-do):

While this can be solved using various automation tools like Apex or Process Builder and Apex, we will use Salesforce Flow and call an Apex method. To call an Apex method, add the Action element to your Salesforce Flow and select an Apex class with a @InvocableMethod Annotation. It means they allow us to extend the Salesforce Flow by writing Apex code that meets certain criteria and then invoking the Apex from our Flows.
 
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.
 
In this article, we will use the call AssignmentRuleHaeder database class from Apex code. Make sure to review it.
 
Before discussing it, 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 4 steps to solve Pamela’s business requirement using Salesforce Flow and Apex. We must:

  1. Setup a lead assignment rule
  2. Create Apex class & Test class
  3. Salesforce before save flow
    1. Define flow properties for record-triggered flow
    2. Add a decision element to check the lead source
    3. Add an assignment element to update status & rating
  4. Salesforce after save flow
    1. Define flow properties for record-triggered flow
    2. Add a scheduled path
    3. Add a decision element to check if lead source changed
    4. Add action – call an Apex class to invoke lead assignment rule

Step 1: Setting Up Lead assignment Rule

  1. Click Setup.
  2. In the Quick Find box, type Lead Assignment Rules.
  3. Click on the Lead Assignment Rules | New button.
  4. Now create an assignment rule, as shown in the following screenshot:

Step 2: Create an Apex class and Test class

Now, we have to understand a new Apex annotation i.e. @InvocableMethod. This annotation lets us use an Apex method as being something that can be called from somewhere other than Apex. The AssignLeadsUsingAssignmentRules class contains a single method that is passing the ids of the Leads whose Lead Source changed to Partner Referral. Create the following class in your organization.

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


Repeat the above steps and click the Test class. You can get the code from my GitHub repo.

Step 3.1: Salesforce Flow – Define Flow Properties for Before-Save Flow

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
    1. How do you want to start building: Freeform
    2. Object: Lead
    3. Trigger the Flow When: A record is created or updated
    4. Set Entry Criteria
      1. Condition Requirements: None
    5. Optimize the Flow For: Fast Field Updates
  5. Click Done.


Tips: Never try to write entry criteria in a Record-Triggered Flow. Why? Check out this article.

Step 3.2: Salesforce Flow – Using Decision Element to Check the Lead Source

Now we will use the Decision element to check the lead source to ensure that it is equal to Partner Referral.

    1. Under Toolbox, select Element.
    2. Drag-and-drop Decision element onto the Flow designer.
    3. Enter a name in the Label field; the API Name will auto-populate.
    4. Under Outcome Details, enter the Label the API Name will auto-populate.
    5. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
      1. Row 1:
        1. Resource: {!$Record.LeadSource}
        2. Operator: Equals
        3. Value: Partner Referral
    6. Click Done.

Step 3.3: Salesforce Flow – Adding an Assignment Element to Update Rating and Status

  1. Under Toolbox, select Element.
  2. Drag-and-drop the Assignment Element element onto the Flow designer.
  3. Enter a name in the Label field- the API Name will auto-populate.
  4. Set Variables Values:
    1. Row 1
      1. Field: {!$Record.Rating}
      2. Value: Hot
  5. Add Condition
  6. Row 2
    1. Field: {!$Record.Status}
    2. Value: Working – Contacted
  7. Click Done.

In the end, Pamela’s Flow will look like the following screenshot (I turned on Auto-Layout) for this flow:
Once everything looks good, perform the steps below:

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 53
  5. Interview Label: Record-Trigger: Lead Before Save {!$Flow.CurrentDateTime}
  6. Click Save.


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

Step 4.1: Salesforce Flow – Define Flow Properties for After-Save Flow

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
    1. How do you want to start building: Freeform
    2. Object: Lead
    3. Trigger the Flow When: A record is created or updated
    4. Set Entry Criteria
      1. Condition Requirements: Only when a record is updated to meet the condition requirements
        1. Field : Lead Source
        2. Operator: Euqals
        3. Value: Partner Referral
    5. Optimize the Flow For: Action and Related Records
  5. Click Done.

Step 4.2: Salesforce Flow – Add Scheduled Paths

  1. Under Start, select Add Scheduled Paths (Optional).
  2. Under SCHEDULED PATHS, click on the New Scheduled Path.
  3. Under Scheduled Path Details, enter the Label the API Name will auto-populate.
  4. Time Source: Lead: Last Modified Date
  5. Offset Number: 1
  6. Offset Options: Minutes After
  7. Click Done.

Step 4.3: Salesforce Flow – Adding an Action to Call Apex class to Trigger Lead Assignment Rule

  1. Under Toolbox, select Element.
  2. Drag-and-drop the Actions element onto the Flow designer.
  3. Select the AssignLeadsUsingAssignmentRules Apex class.
  4. Enter a name in the Label field- the API Name will auto-populate.
  5. Set Input Values:
    1. Field: LeadIds
    2. Value: {!$Record.Id}
  6. Click Done.


In the end, Pamela’s Flow will look like the following screenshot (I turned on Auto-Layout) for this flow:


Once everything looks good, perform the steps below:

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 53
  5. Interview Label: Record-Trigger: Lead After Save {!$Flow.CurrentDateTime}
  6. Click Save.


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

Proof of Concept

Now onward, if a business user updates the Lead Source to Partner Referral, Process Builder will automatically update Status, Type, and Assign it to the right user or queue based on the lead assignment rule.

  1. Currently, the lead Beverly Burks is Open and the Lead Source is Blank as shown in the following screenshot:
  2. Now we update the Lead Source to Partner Referral and wait for a minute to see the process builder magic.

Monitor Your Schedule Flow

To monitor Flows that are scheduled, navigate to the following path:

  1. Navigate to Setup (Gear Icon) | Environments | Monitoring | Time-Based Workflow.
  2. Now look for your Scheduled Flow job displaying information as shown in the following screenshot:
  3. Use the Delete button to delete the time-based Flow job from the queue.

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 at @automationchamp, or find me on LinkedIn.

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

One thought on “Getting Started with Salesforce Flow – Part 77 (Running Lead Assignment Rules From Salesforce Flow)

Leave a Reply

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