Last Updated on February 14, 2022 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):
Guided Practice (We-do):
There are 4 steps to solve Pamela’s business requirement using Salesforce Flow and Apex. We must:
- Setup a lead assignment rule
- Create Apex class & Test class
- Salesforce before save flow
- Define flow properties for record-triggered flow
- Add a decision element to check the lead source
- Add an assignment element to update status & rating
- Salesforce after save flow
- Define flow properties for record-triggered flow
- Add a scheduled path
- Add a decision element to check if lead source changed
- Add action – call an Apex class to invoke lead assignment rule
Step 1: Setting Up Lead assignment Rule
- Click Setup.
- In the Quick Find box, type Lead Assignment Rules.
- Click on the Lead Assignment Rules | New button.
- 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.
- Click Setup.
- In the Quick Find box, type Apex Classes.
- Click on the New button.
- Copy code from GitHub and paste it into your Apex Class.
- 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
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the New Flow.
- Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
- How do you want to start building: Freeform
- Object: Lead
- Trigger the Flow When: A record is created or updated
- Set Entry Criteria
- Condition Requirements: None
- Optimize the Flow For: Fast Field Updates
- 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.
-
- Under Toolbox, select Element.
- Drag-and-drop Decision element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!$Record.LeadSource}
- Operator: Equals
- Value: Partner Referral
- Row 1:
- Click Done.
Step 3.3: Salesforce Flow – Adding an Assignment Element to Update Rating and Status
- Under Toolbox, select Element.
- Drag-and-drop the Assignment Element element onto the Flow designer.
- Enter a name in the Label field- the API Name will auto-populate.
- Set Variables Values:
- Row 1
- Field: {!$Record.Rating}
- Value: Hot
- Row 1
- Add Condition
- Row 2
- Field: {!$Record.Status}
- Value: Working – Contacted
- 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:
- Click Save.
- Enter Flow Label the API Name will auto-populate.
- Click Show Advanced.
- API Version for Running the Flow: 53
- Interview Label: Record-Trigger: Lead Before Save {!$Flow.CurrentDateTime}
- Click Save.
Almost there! Once everything looks good, click the Activate.
Step 4.1: Salesforce Flow – Define Flow Properties for After-Save Flow
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the New Flow.
- Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
- How do you want to start building: Freeform
- Object: Lead
- Trigger the Flow When: A record is created or updated
- Set Entry Criteria
- Condition Requirements: Only when a record is updated to meet the condition requirements
- Field : Lead Source
- Operator: Euqals
- Value: Partner Referral
- Condition Requirements: Only when a record is updated to meet the condition requirements
- Optimize the Flow For: Action and Related Records
- Click Done.
Step 4.2: Salesforce Flow – Add Scheduled Paths
- Under Start, select Add Scheduled Paths (Optional).
- Under SCHEDULED PATHS, click on the New Scheduled Path.
- Under Scheduled Path Details, enter the Label the API Name will auto-populate.
- Time Source: Lead: Last Modified Date
- Offset Number: 1
- Offset Options: Minutes After
- Click Done.
Step 4.3: Salesforce Flow – Adding an Action to Call Apex class to Trigger Lead Assignment Rule
- Under Toolbox, select Element.
- Drag-and-drop the Actions element onto the Flow designer.
- Select the AssignLeadsUsingAssignmentRules Apex class.
- Enter a name in the Label field- the API Name will auto-populate.
- Set Input Values:
- Field: LeadIds
- Value: {!$Record.Id}
- 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:
- Click Save.
- Enter Flow Label the API Name will auto-populate.
- Click Show Advanced.
- API Version for Running the Flow: 53
- Interview Label: Record-Trigger: Lead After Save {!$Flow.CurrentDateTime}
- 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.
- Currently, the lead Beverly Burks is Open and the Lead Source is Blank as shown in the following screenshot:
- 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:
- Navigate to Setup (Gear Icon) | Environments | Monitoring | Time-Based Workflow.
- Now look for your Scheduled Flow job displaying information as shown in the following screenshot:
- 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.
after the apex class fires, noticed the lead owner is assigned to default lead owner, instead of using lead assignment rule. Any clue?
Thank you for an excellent tutorial 🙂 you solved my problem! Very much appreciated
Anyone getting issues with an error on mass updates “Apex error occurred: System.QueryException: List has more than 1 row for assignment to SObject “? if each one is called individually, I don’t understand how there is more than 1 row for assignment. Sometimes I get an email with this error only to see that the trigger actually worked for the specified record so a bit odd. Thanks!
Thank you for the great tutorial. Why add the 1 minute wait? Is that just to take avoid too much synchronous automation? Or is it required for another reason?
You’re right Kevin (to make the process asynchronous).