Sub topic 5
Building lead assignment rules
This chapter assumes you can navigate Setup, know what a queue is, and can read basic Apex. It covers how a lead assignment rule is structured, how to build one, and the errors that most often break it. The next chapter covers when rules run and how to trigger them.
What an assignment rule is
A lead assignment rule is an ordered list of conditions that decides who owns a new or updated lead. Each condition is a rule entry. An entry pairs filter criteria with an owner, which is either a user or a queue.
Three facts shape everything else:
- Only one assignment rule can be active at a time for leads.
- Within the active rule, Salesforce evaluates entries by sort order and stops at the first match.
- If no entry matches, the lead goes to the default lead owner, or to the user who created it if no default owner is set.
How evaluation works
The diagram shows how a lead moves through the active rule.
Because the first match wins, order matters. Put specific entries first and broad entries last.
Build a rule
- In Setup, enter Lead Assignment Rules in Quick Find and open it.
- Click New, name the rule (for example,
Standard Routing), and check Set this as the active lead assignment rule. - Save, then open the rule and click New in the Rule Entries list.
- Enter a Sort Order, the position in which the entry is evaluated.
- Choose the criteria. You can filter by field values or use a formula.
- Select the owner: a user or a queue.
- Optionally pick an email template to notify the new owner.
- Save the entry and repeat for each routing case.
A sample rule for a sales team might look like this.
| Sort order | Criteria | Assign to |
|---|---|---|
| 1 | Country equals `Germany` | DACH Queue |
| 2 | Annual Revenue greater than 1,000,000 | Enterprise Queue |
| 3 | Lead Source equals `Web` | Inbound Queue |
| 4 | (no criteria) | Unassigned Queue |
An entry with no criteria matches every lead. That makes it a useful catch-all as the final entry. Placed anywhere else, it hides every entry after it.
Choosing between filters and formulas
Filter criteria are easier to read and maintain. Use a formula when you need logic that filters cannot express, such as comparing two fields or using OR across different fields.
OR(
ISPICKVAL(Industry, "Banking"),
ISPICKVAL(Industry, "Insurance")
)Formula criteria are harder to audit, so prefer filters for simple routing.
Common errors
- Rule is saved but nothing changes. Creating an active rule does not apply it to existing leads. A rule runs only when a save triggers it, for example through a checkbox on the lead form, Web-to-Lead, or Apex with
DMLOptions. - Leads land on the wrong owner. An earlier, broader entry matched first. Reorder the entries.
- Leads go to the default owner. No entry matched. Check for typos in picklist values and for blank fields. A criterion such as
State equals CAdoes not match a lead whose state is empty. - Cannot assign to a queue. The queue must list Lead as a supported object. Open the queue and add it.
- Cannot activate a second rule. Activating a rule deactivates the current one. Edit the existing rule instead of creating parallel ones.
Check your work
Create a test lead and trigger the rule from Apex, as shown here.
Lead l = new Lead(
FirstName = 'Ada',
LastName = 'Lovelace',
Company = 'Analytical Engines Ltd',
Country = 'Germany',
LeadSource = 'Web'
);
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
l.setOptions(dmo);
insert l;
Lead saved = [SELECT Owner.Name FROM Lead WHERE Id = :l.Id];
System.debug('Owner: ' + saved.Owner.Name);With the sample rule above, the debug log should show DACH Queue. If it shows something else, compare the lead's field values to each entry in order.
Exercise
A rule has entry 1 (Lead Source equals Web, assign to Inbound Queue) and entry 2 (Country equals France, assign to France Queue). A French lead arrives through the web. Which queue owns it, and how would you make it go to the France Queue?
Show answer
It goes to the Inbound Queue, because entry 1 matches first and evaluation stops. To route it to the France Queue, change the sort order so the Country entry is evaluated before the Lead Source entry.
Exercise
You created a new active assignment rule, but leads that already existed still have their old owners. Why?
Show answer
Assignment rules run only when a save triggers them. Creating or activating a rule does not reassign existing leads. You must save those leads with the assignment rule option enabled, for example with Apex using DMLOptions.