Sub topic 9
Assignment, auto-response, workflow, and escalation rules
This chapter covers the step that runs after after triggers and before after-save flows. It assumes you can write a basic trigger, know validation rules and Flow, and understand DML.
Where these rules run
After the after triggers finish, Salesforce evaluates a group of declarative rules in a fixed sequence:
- Assignment rules
- Auto-response rules
- Workflow rules
- Escalation rules
- Entitlement rules
Which of these run depends on the object. Assignment rules apply to Lead and Case. Auto-response rules apply to Lead and Case, for example the email sent after a web-to-case submission. Escalation and entitlement rules apply to Case only. Workflow rules apply to most objects that support them.
What each rule does
| Rule | Typical object | Effect |
|---|---|---|
| Assignment | Lead, Case | Sets the record owner based on criteria |
| Auto-response | Lead, Case | Sends an email to the submitter |
| Workflow | Many objects | Field updates, email alerts, tasks, outbound messages |
| Escalation | Case | Reassigns or notifies when a case stays open too long |
| Entitlement | Case | Applies support terms and milestones |
Why the order matters to your triggers
Assignment rules run after your after trigger. An after trigger that reads OwnerId on a new Case or Lead may therefore see the owner before the rule changes it. Assignment rules also run only when the save asks for them. This happens through the "Assign using active assignment rules" checkbox in the UI, web-to-lead and web-to-case, or the assignmentRuleHeader in the API. In Apex you must set it yourself:
Database.DMLOptions opts = new Database.DMLOptions();
opts.assignmentRuleHeader.useDefaultRule = true;
Case c = new Case(Subject = 'Printer offline', Origin = 'Web');
c.setOptions(opts);
insert c;Without setOptions, an Apex insert does not run the assignment rule.
Field updates can run your triggers again
A workflow field update that changes a value causes the before update and after update triggers to fire a second time. Validation rules do not run again, and neither do the earlier steps such as before-save flows on that second pass. Only the before and after triggers (and the custom validation that follows them) are repeated. This is the cause of many duplicate-processing bugs. The next chapter covers it in detail.
Common errors
- Expecting an Apex insert to assign an owner through the active assignment rule without setting
DMLOptions. - Reading
OwnerIdin an after trigger and assuming it is final. - Adding a workflow field update to an object whose trigger is not guarded against running twice.
Exercise
A Case is inserted from Apex with no DMLOptions. An after insert trigger logs OwnerId, and an active assignment rule would route the case to a queue. Which owner does the trigger see, and does the assignment rule run?
Show answer
The trigger sees the owner that was set on the record (by default the running user). The assignment rule does not run, because the Apex insert did not enable it through DMLOptions with assignmentRuleHeader.useDefaultRule set to true. Even when it is enabled, the rule runs after the after trigger, so the trigger still sees the pre-assignment owner.