Sub topic 6
When assignment rules run, and how to run them
This chapter assumes you can build a lead assignment rule with ordered rule entries (the previous chapter) and that you can read basic Apex. Here you will learn which entry points run assignment rules automatically, which ones do not, and how to force a run.
The core idea
An assignment rule does nothing on its own. Salesforce evaluates it only when the save operation asks for it. Only one rule can be active at a time, and "the active rule" is what every trigger below refers to.
When a lead is saved with the rule enabled, Salesforce checks the rule entries in order. The first entry whose criteria match sets the owner. If no entry matches, the lead goes to the default lead owner from your lead settings, or stays with the user who created it.
Where rules run
| Entry point | Runs the active rule by default? | How to control it |
|---|---|---|
| Web-to-Lead form submission | Yes | The rule runs for each submitted lead. |
| New Lead in Lightning Experience | Depends on layout | Page layout and Lead settings. The user can pick the option when saving in some layouts. |
| Edit an existing lead | No | The user must choose to run the rule. |
| Data Import Wizard | Optional | Choose the assignment rule option during import. |
| Apex `insert` or `update` | No | Set `Database.DMLOptions`. |
| API (SOAP or REST) | No | Send the assignment rule header or option. |
Treat the defaults in this table as a starting point. Your org's page layouts and settings can change the UI behavior, so test in a sandbox.
Running rules from the UI
In Lightning Experience, the New Lead form can include a "Assign using active assignment rule" checkbox. Add this field to the page layout if it is missing. For existing leads, a user who has permission can re-run the rule with this checkbox when editing the lead.
For bulk work, the Data Import Wizard lets you choose the active assignment rule for the whole file. This is the usual way to route imported leads.
Web-to-Lead
Web-to-Lead submissions run the active rule automatically. Salesforce creates the lead from the form post and applies the rule, so you need no extra setting. The form still needs a valid lead status and source, which you set in the previous chapters, and the org must stay within the daily Web-to-Lead limit.
Running rules from Apex
Apex DML does not use assignment rules unless you opt in. Create a Database.DMLOptions object and set useDefaultRule to true.
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;setOptions attaches the options to that one record, and they apply to the next DML statement that includes it. The same call works with update, so you can re-run the rule on existing leads.
You can also pass the options to the Database methods, such as Database.insert(l, dmo), which is useful when you want partial success results.
To pick a specific rule instead of the active one, set assignmentRuleId on the same header and leave useDefaultRule as false.
Common errors
- Owner does not change after Apex insert. The DMLOptions were never set, or
setOptionswas called after the DML statement. - Owner does not change after edit. Nobody chose to run the rule on that save.
- Lead lands with the wrong owner. An earlier rule entry matched first. Check the entry order.
- Lead goes to the default owner. No entry matched, or the entry points to an inactive user.
- Bulk update is slow. Running rules on many records adds work. Use batches of a manageable size.
Exercise
An Apex trigger inserts leads for a partner integration, but every lead is owned by the integration user, even though your rule has matching entries. What is missing, and what is the fix?
Show answer
Apex DML does not run assignment rules by default. Create a Database.DMLOptions, set assignmentRuleHeader.useDefaultRule = true, and attach it with setOptions (or pass it to Database.insert) before the insert.
Exercise
A sales rep changes a lead's State from Texas to Ohio. Your entries route by state, but the owner is unchanged. Why, and how can the rep fix it?
Show answer
Editing a lead does not re-run assignment rules unless the save requests it. The rep should edit the lead again and select the option to assign using the active assignment rule, or an admin can run the rule through Apex or a data tool.