Sub topic 5
Building Lead Assignment Rules
This chapter assumes you can navigate Setup and the Object Manager, know how users, profiles, and queues relate to record ownership, and can read basic Apex. You will build an assignment rule with ordered rule entries, learn how Salesforce evaluates them, and see the errors that cause leads to land with the wrong owner.
What an assignment rule does
A lead assignment rule is a set of ordered conditions that decides who owns a new or updated lead. The owner can be a user or a queue. The rule does not move leads on its own schedule. It runs only when something triggers it, which the next chapter covers.
A rule has two levels:
- The rule is a named container. You can create many rules for Leads, but only one can be active at a time.
- A rule entry is one row in the rule. It holds a sort order, filter criteria, an assignee, and an optional email template.
Salesforce evaluates the entries of the active rule from the lowest sort order to the highest. The first entry whose criteria match the lead wins. Salesforce assigns the lead to that entry's assignee and stops evaluating.
Prerequisites in the org
Before you build a rule, create the owners it will point to.
- Create the queues you need (for example
Enterprise LeadsandSMB Leads) and make sure Lead is a supported object for each queue. - Confirm the users you plan to assign to are active and have access to the Lead object.
- Decide on a default lead owner in Lead Settings. This is the fallback when no entry matches, so it matters more than it looks.
Create the rule
- In Setup, enter
Lead Assignment Rulesin Quick Find and open it. - Click New.
- Enter a name, such as
Regional Lead Routing. - Select Set this as the active lead assignment rule if you want it live immediately. Leave it cleared while you are still building.
- Click Save.
Saving an empty rule is safe, but activating one with no entries means no lead will match anything.
Add rule entries
Open the rule and click New in the Rule Entries section. Each entry has these parts:
| Part | Purpose |
|---|---|
| Sort Order | Position in the evaluation sequence. Lower numbers run first. |
| Rule Criteria | Either filter criteria (field, operator, value) or a formula that returns true or false. |
| Assign To | A user, a queue, or the Lead Owner role-based option. |
| Email Template | Optional template used to notify the new owner. |
| Do Not Reassign Owner | When selected, a matching lead keeps its current owner and the rule stops there. |
Choosing between criteria and a formula
Use filter criteria for simple conditions. Two or more criteria on one entry are combined with AND by default. You can change the filter logic to add OR groups, for example 1 AND (2 OR 3).
Use a formula when the condition needs functions or cross-field logic. This formula matches leads from large companies in two states:
AND(
NumberOfEmployees >= 1000,
OR(State = "CA", State = "NY")
)Formulas are more flexible but harder to audit. Filter criteria show up clearly in the entry list, so prefer them when they are enough.
A worked rule
Suppose the sales team routes leads like this: large companies go to an enterprise queue, anything from the West region goes to a regional rep, and everything else goes to a general queue.
| Sort Order | Criteria | Assign To |
|---|---|---|
| 1 | `Lead: Annual Revenue` greater than or equal to 50000000 | Queue: Enterprise Leads |
| 2 | `Lead: State/Province` equals `CA,OR,WA` | User: West Rep |
| 3 | `Lead: Lead Source` equals `Web` | Queue: Inbound Web Leads |
| 4 | (no criteria) | Queue: General Leads |
Entry 4 has no criteria, so it matches every lead that reached it. This is the standard catch-all pattern, and it should always be last. Without it, an unmatched lead falls back to the default lead owner, which is often an administrator who never works leads.
The order is part of the logic. A California lead from a company with 60 million in annual revenue matches entry 1 and never reaches entry 2. If you want regional routing to take precedence, swap the sort orders. Reorder entries by editing the Sort Order values, not by deleting and recreating them.
Entries can match a comma-separated list of values in one filter, as in the CA,OR,WA example, which keeps the entry count small.
Assigning to a queue or a user
When you pick a queue as the assignee, the lead's owner becomes the queue. Users in the queue (and anyone above them in the role hierarchy, depending on sharing settings) can see the lead and take ownership. Queue ownership works well for shared inboxes. Assigning to a single user suits named territories but creates a single point of failure when that user leaves.
If you assign to a user, make sure that user is active. An inactive user cannot be selected for new entries, and deactivating one later leaves the entry pointing at a user who can no longer work the lead.
Activate and test
Only one lead assignment rule can be active. Activating a different rule deactivates the current one. To change live behavior safely:
- Clone your current rule so you have a rollback.
- Edit the clone, with the original still active.
- Test the clone with a sample lead (see below).
- Activate the clone when it behaves correctly.
To test, create a lead from the UI with Assign using active assignment rules selected, then check the Owner field. Build one test lead for each entry, including one that matches only the catch-all.
Running the rule from Apex
An assignment rule does not run for Apex inserts unless you ask it to. This is the most common reason a rule works in the UI but not in an integration or a trigger. Use Database.DMLOptions to request the active rule:
Lead l = new Lead(
FirstName = 'Ada',
LastName = 'Lovelace',
Company = 'Analytical Engines Ltd',
Email = '[email protected]',
LeadSource = 'Web'
);
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
l.setOptions(dmo);
insert l;useDefaultRule = true means "use the active rule." You can instead set assignmentRuleId to run a specific rule by ID, which is useful for testing a rule that is not active. Setting both is an error, so pick one.
The same options object works with Database.insert(l, dmo), which is convenient when you also want to inspect SaveResult objects.
Edge cases and common errors
- No match and no default. If no entry matches and the default lead owner is not set sensibly, leads land with an unintended owner. Always end with a catch-all entry.
- Rule did not run. The lead was created or edited without the assignment option, or an Apex insert had no
DMLOptions. Check how the lead was saved before you debug the rule. - Wrong entry wins. Entries are evaluated by sort order, and a broad entry placed early shadows narrower ones. Put specific entries first and general ones last.
- Case and spelling in filters. Values in filter criteria must match the field values. For picklists, a renamed or inactive value no longer matches. Review entries after you change picklist values.
- Blank fields. A criterion such as
State not equal to CAalso matches leads where State is blank. Add an explicit "not equal to null" condition if you do not want that. - Do Not Reassign Owner. When selected on a matching entry, the lead keeps its owner. This is useful for leads that are already worked, but it makes the rule appear broken if you forget it is set.
- Reassignment on edit. Re-running the rule on an existing lead can take it away from a rep who is working it. Use the assignment option on edits deliberately.
Summary of the working model
An assignment rule is an ordered list of conditions with one owner each. The first match wins, the last entry should catch everything else, and the rule runs only when something asks it to. Most problems come from entry order, missing catch-alls, and forgetting to trigger the rule.
Exercise
A lead from Oregon with 60 million in annual revenue is created in the UI with "Assign using active assignment rules" selected. Using the worked rule in this chapter, who owns the lead? What single change would route it to the West Rep instead?
Show answer
It is owned by the Enterprise Leads queue, because entry 1 (annual revenue of 50,000,000 or more) matches first and evaluation stops. To route it to the West Rep, change the sort orders so the state entry is evaluated before the revenue entry (for example, make the state entry 1 and the revenue entry 2).
Exercise
An integration inserts leads with Apex, and all of them end up owned by the integration user, even though the active rule would assign them to queues. What is the likely cause, and how do you fix it?
Show answer
Assignment rules do not run on Apex inserts by default. Create a Database.DMLOptions object, set assignmentRuleHeader.useDefaultRule to true, and attach it with l.setOptions(dmo) (or pass it to Database.insert) before inserting.