Sub topic 10
Assignment, auto-response, workflow, and escalation rules
This chapter assumes you know the order of execution up to after triggers, and that you have written a basic Apex trigger. It covers the group of rules that runs right after after triggers. These rules are mostly declarative. Several of them do not run on a save unless you ask for them, and one of them can cause triggers to fire again.
Where these rules sit
After the record is saved to the database (not yet committed) and after triggers have run, Salesforce evaluates these rules in this order:
- Assignment rules
- Auto-response rules
- Workflow rules
- Escalation rules
- Entitlement rules
Your triggers therefore run before these rules. A trigger cannot see an owner assigned by an assignment rule, because the rule has not run yet.
What each rule does
| Rule | Objects | What it does | When it runs |
|---|---|---|---|
| Assignment | Lead, Case | Sets the record owner (a user or a queue) | Only when requested |
| Auto-response | Lead, Case | Sends an email reply to the person who submitted the record | Only when requested or from web and email channels |
| Workflow | Most standard and custom objects | Runs field updates, email alerts, tasks, and outbound messages | When the rule criteria match |
| Escalation | Case | Reassigns or notifies when a case stays open too long | Evaluated at save, acts on a timer |
| Entitlement | Case, Work Order | Applies a support entitlement and milestones | When entitlement settings are in use |
Assignment and auto-response rules do not run by default
An Apex insert or update does not apply assignment or auto-response rules on its own. They run in these cases:
- The record comes from Web-to-Lead, Web-to-Case, or Email-to-Case.
- A user ticks Assign using active assignment rules in the UI.
- Your Apex code sets DML options.
Here is how to request both from Apex:
Lead l = new Lead(LastName = 'Rivera', Company = 'Acme');
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
dmo.emailHeader.triggerAutoResponseEmail = true;
l.setOptions(dmo);
insert l;useDefaultRule = true selects the active assignment rule. Without it, the new lead keeps the owner that was on the record, which is normally the running user.
The same options work with Database.insert(l, dmo).
Workflow rules
A workflow rule has criteria and actions. The criteria are checked after the record is saved, and the evaluation setting decides whether the rule runs only on creation or on every edit.
- Field updates change the record immediately, inside the transaction.
- Email alerts and outbound messages are queued and sent at commit.
- Tasks are created as part of the transaction.
Workflow rules are a legacy tool. Salesforce stopped allowing new workflow rules and new Process Builder processes in Winter '23. Existing rules still run, so you will meet them in older orgs, and they still affect ordering.
The field update that fires triggers again
A workflow field update changes the record after your triggers have already run. When that happens, the before update and after update triggers fire one more time. Your update logic can therefore run twice in one transaction. That is why the chapter on recursion shows a static Set<Id> guard.
Look at this trigger:
trigger CaseAfterUpdate on Case (after update) {
System.debug('Case after update, entry count: ' + Limits.getQueries());
}If a workflow rule on Case updates a field, this trigger logs twice for one user save. The second run sees the field value that the workflow wrote.
Two details help you predict this:
- The second pass happens only if a field update actually changed a value.
- Custom validation rules and duplicate rules do not run again on the second pass.
Escalation rules
Escalation rules apply only to Case. When you save a case, Salesforce checks which escalation rule entries match. The escalation action itself does not happen during your save. It happens later, when the case has stayed open past the age limit in the rule. Your transaction is not blocked by that wait, and the later action is a separate transaction.
Entitlement rules
Entitlement rules apply an entitlement to a case when the org uses entitlement management. They run last in this group. Most orgs that do not use entitlements never see this step.
Practical consequences
- Triggers see the record before assignment rules change the owner. If you need the final owner, do the work in a later step or in a separate transaction.
- Email alerts from workflow rules go out only if the transaction commits. If a later step throws an error, no email is sent.
- Limits are shared. SOQL queries and DML statements that workflow-fired triggers use count against the same transaction totals.
- When a workflow field update is the only reason a trigger runs twice, a before-save flow that sets the same field is often a cleaner replacement, because it needs no second save.
Exercise
A user saves a Case from the UI without ticking the assignment rule checkbox. A workflow rule on Case updates the Status field. Does an assignment rule run, and how many times does your after update trigger run?
Show answer
No assignment rule runs, because assignment rules run only when requested (UI checkbox, web channel, or DML options). The after update trigger runs twice: once for the user's save, and once more because the workflow field update changed a value.