Sub topic 6
When Assignment Rules Run, and How to Run Them
Assignment rules only run when something tells them to. This chapter covers the places where a lead can be created or edited, which of them trigger a rule, and how to trigger one from the UI, from Web-to-Lead, and from Apex. It ends with the common reasons a rule appears not to fire.
This chapter assumes you can navigate Setup and the Object Manager. It also assumes you have already built a lead assignment rule with ordered rule entries, as covered in the previous chapter, and that you can read basic Apex.
The core idea: rules are opt-in per save
A lead assignment rule is not a trigger that fires on every insert or update. Salesforce evaluates a rule only when the save request asks for it. If the request does not ask, the lead keeps whatever owner it already has. For a new record, that is the user who created it, unless a default owner is set by another mechanism.
Three facts follow from this:
- Only one assignment rule can be active for leads at a time. When a request asks for "the active rule," this is the one that runs.
- The rule runs at save time. It does not re-evaluate leads later, so changing a rule does not move existing leads.
- Whether the rule runs depends on how the lead entered Salesforce, not on the rule's contents.
Entry points and their defaults
The table below summarizes the common ways a lead is created or edited, and whether the active rule runs by default.
| Entry point | Runs the active rule by default? | How to change it |
|---|---|---|
| Lead edit page in the UI (new lead) | No, unless the checkbox is selected or the layout default applies | Select "Assign using active assignment rule" |
| Lead edit page in the UI (existing lead) | No | Select "Assign using active assignment rule" |
| Web-to-Lead form | Yes | Not configurable per submission |
| Web-to-Case or other integrations through the API | No | Set the assignment rule header |
| Data Import Wizard | Only if you choose a rule during import | Pick the rule in the wizard |
| Data Loader | No | Enable the assignment rule option in settings |
| Apex DML | No | Set `Database.DMLOptions` |
| Flows and Process Builder | Depends on the record save | Use a Flow-specific option or Apex action |
Treat the defaults above as a starting point and verify them in your own org. Behavior for some tools differs by release and by configuration.
Running the rule from the UI
On the lead edit page, the "Assign using active assignment rule" checkbox sits near the owner field. It is not part of the lead's data. It is a one-time instruction that applies to that save only.
Some points to keep in mind:
- The checkbox only appears if it is on the page layout. If users cannot see it, an admin can add it to the layout.
- Selecting the checkbox overrides the owner chosen manually in the same save. The rule's result wins.
- If no rule entry matches the lead, the lead goes to the default lead owner defined in the rule. If no active rule exists, the checkbox has no effect.
Salesforce Classic and Lightning Experience both support this option. In Lightning, check the page layout of the lead record rather than a Lightning page when you cannot find the checkbox.
Reassigning in bulk
A list view can change owners in bulk, but it does not run the rule. To rerun the rule across existing leads, use one of these approaches:
- Export the leads and re-import them with the assignment rule option turned on, using the import tool or Data Loader.
- Run an Apex script that updates the leads with
DMLOptionsset. - Use a Flow or other automation that calls Apex to do the same.
Updating a field to the same value is enough to make Apex re-save the record, but the rule runs only if the options are attached.
Web-to-Lead
Web-to-Lead creates a lead from a form on your website. Leads created this way are assigned by the active rule automatically. You do not need to add anything to the form.
Two things commonly surprise people:
- Leads that match no rule entry go to the default lead owner in the rule, which must be set when the rule is created. Make sure it is a user or queue that someone actually monitors.
- The form posts to Salesforce, so a hidden field in the HTML cannot control the rule. If you want different handling for different forms, put the logic in rule entries. A hidden field such as Lead Source or a custom field can carry the form identity, and the rule entry can then match on it.
<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type="hidden" name="oid" value="00Dxx0000000000">
<input type="hidden" name="retURL" value="https://example.com/thanks">
<input type="hidden" name="lead_source" value="Web">
<label for="first_name">First name</label>
<input id="first_name" name="first_name" type="text">
<label for="last_name">Last name</label>
<input id="last_name" name="last_name" type="text">
<label for="company">Company</label>
<input id="company" name="company" type="text">
<label for="email">Email</label>
<input id="email" name="email" type="email">
<input type="submit" value="Submit">
</form>Replace the oid value with your own organization ID. The HTML form is generated for you in Setup under Web-to-Lead, so use the generated version in practice. The example above only shows the shape.
A Web-to-Lead org also has a daily limit on submissions. Excess submissions are queued or dropped according to the platform's rules, so check the current limit in the Salesforce documentation if you expect high volume.
Running the rule from Apex
Apex DML does not run assignment rules unless you attach options. You attach them with Database.DMLOptions, and you can do so in two ways.
Using the active rule
This sets the options on the record and then performs the insert. It is the same example you saw in the tutorial's code samples.
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." setOptions attaches the options to that record, and the following insert honors them.
Using a specific rule
You can instead name a rule by its ID. This is useful when more than one rule exists, even though only one is active.
AssignmentRule ar = [
SELECT Id
FROM AssignmentRule
WHERE SobjectType = 'Lead' AND Active = true
LIMIT 1
];
Lead l = new Lead(
FirstName = 'Grace',
LastName = 'Hopper',
Company = 'Compiler Works',
LeadSource = 'Web'
);
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.assignmentRuleId = ar.Id;
l.setOptions(dmo);
insert l;An assignmentRuleHeader setting accepts either useDefaultRule or assignmentRuleId. Use one of them, not both.
Using the Database methods
When you call Database.insert or Database.update, you can pass the options directly instead of calling setOptions on each record. This is the better pattern for lists.
List<Lead> leads = new List<Lead>{
new Lead(LastName = 'Turing', Company = 'Computing Ltd', LeadSource = 'Web'),
new Lead(LastName = 'Hamilton', Company = 'Apollo Software', LeadSource = 'Web')
};
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
List<Database.SaveResult> results = Database.insert(leads, dmo);
for (Database.SaveResult sr : results) {
if (!sr.isSuccess()) {
System.debug('Failed: ' + sr.getErrors()[0].getMessage());
}
}Because Database.insert with options returns a result per record, you can inspect failures without an exception ending the whole transaction.
Triggers and the owner field
A common mistake is to set OwnerId in code and also attach assignment options, then wonder why the owner value changes. When the rule runs, it sets the owner itself, replacing whatever you set. If you want to keep a manually assigned owner, do not attach the options.
Apex triggers on Lead run in the same save. In a before-insert trigger, OwnerId may not yet reflect the rule result, because the assignment happens after before triggers in the save sequence. Do not rely on the rule's chosen owner inside a before trigger. Read it in an after trigger, or query the record after the DML statement finishes.
Why a rule seems not to run
When an owner looks wrong, work through these causes in order.
- No active rule. Check that a lead rule is marked active. Creating a rule does not activate it.
- The request did not ask for the rule. For UI saves, the checkbox was cleared. For Apex, the options were not attached or were attached to a different record.
- An earlier entry matched. Entries are evaluated in order, and the first match wins. A broad entry placed above a narrow one hides the narrow one.
- Criteria compare values exactly. A picklist value that was renamed, or a country entered as free text, may not match the entry.
- The default owner caught it. The lead matched no entry and went to the default lead owner, which can look like the rule did nothing.
- Later automation changed the owner. A flow, trigger, or other process ran after assignment and wrote a new
OwnerId. - The assignee cannot own leads. A queue must list Lead as a supported object, and a user must have access to leads. Otherwise the save fails or the assignment is rejected.
Putting it together
The decision about whether a rule runs is made by the request, so the same lead can end up with different owners depending on how it was saved. For a predictable process, choose one path per source and document it:
- Web-to-Lead for website forms, since it runs the rule by default.
- The checkbox or an import option for manual and bulk entry.
DMLOptionsin any Apex or integration code that creates leads.
Exercise
An integration creates leads through Apex with insert leads; and every lead is owned by the integration user. The active assignment rule is correct. What is wrong, and what is the smallest change that fixes it?
Show answer
Plain Apex DML does not run assignment rules unless options are attached, so the leads keep the creating user as owner. Create a Database.DMLOptions object, set assignmentRuleHeader.useDefaultRule = true, and call Database.insert(leads, dmo) instead of insert leads;. Use one DMLOptions instance for the whole list.
Exercise
A lead is saved with the "Assign using active assignment rule" checkbox selected and the owner manually set to a different user. Which owner does the lead have afterward, and why?
Show answer
The owner chosen by the rule. When the save asks for the rule, the rule's result replaces the manually selected owner. If no entry matches, the default lead owner from the rule is used.
Exercise
In a before-insert Lead trigger, you read OwnerId expecting the rule's result and see the creating user. Why, and where can you read the assigned owner reliably?
Show answer
Assignment happens after before triggers run in the save sequence, so OwnerId has not been changed yet. Read it in an after trigger, or query the lead after the DML statement completes.