Sub topic 20
Confirming the order with debug logs
Earlier chapters described the order of execution from documentation. This chapter shows how to confirm it in your own org. A debug log is a text record of one transaction, and the order of its lines is the order Salesforce ran your automation. This chapter assumes you can write a basic trigger, read Apex, and open Setup. It also assumes you have run anonymous Apex from the Developer Console.
What a debug log records
A debug log captures the events of one transaction for a user you choose. Each line has a timestamp, an event name, and details, separated by |. The events that matter for ordering are:
| Event | What it tells you |
|---|---|
| `CODE_UNIT_STARTED` / `CODE_UNIT_FINISHED` | A trigger, a workflow evaluation, or other unit of work began or ended |
| `USER_DEBUG` | Output from your own `System.debug` calls |
| `DML_BEGIN` / `DML_END` | A DML statement ran, with the operation and row count |
| `VALIDATION_RULE`, `VALIDATION_PASS`, `VALIDATION_FAIL` | A validation rule was evaluated, and its result |
| `WF_RULE_EVAL_BEGIN`, `WF_FIELD_UPDATE` | A workflow rule was evaluated, and a field update was applied |
| `FLOW_START_INTERVIEWS_BEGIN` | A flow started running |
| `CUMULATIVE_LIMIT_USAGE` | Governor limit totals for the transaction |
Nesting matters as much as order. A trigger fired by a DML statement inside another trigger appears between that trigger's CODE_UNIT_STARTED and CODE_UNIT_FINISHED lines.
Set up logging
- In Setup, search for Debug Logs.
- Add a trace flag for your own user. A trace flag is the rule that turns logging on for a user for a limited period.
- Pick or create a debug level. A debug level sets how much detail each log category records.
- Clear old logs so you only see the one you want.
Set Apex Code to DEBUG, Workflow to INFO or higher, Validation to INFO, and Database to INFO. Set System to DEBUG if you want to see limit totals. Lower levels hide events, and a missing event often just means a category was set too low.
Instrument a trigger
Add System.debug lines so your own markers sit among the system events. This uses the trigger from the spine, with the same logic.
trigger AccountOrderDemo on Account (before insert, before update, after insert, after update) {
if (Trigger.isBefore) {
System.debug('BEFORE ' + Trigger.operationType);
for (Account a : Trigger.new) {
if (a.Description == null) {
a.Description = 'Set in before trigger';
}
}
} else {
System.debug('AFTER ' + Trigger.operationType);
}
}Open Debug > Open Execute Anonymous Window in the Developer Console and run this:
Account a = new Account(Name = 'Order Test');
insert a;
System.debug('Saved Id: ' + a.Id);Read the log
Open the log from the Logs tab. An abridged excerpt looks like this. Timestamps and Ids are removed, and your lines will differ in detail.
CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
DML_BEGIN|[2]|Op:Insert|Type:Account|Rows:1
CODE_UNIT_STARTED|[EXTERNAL]|AccountOrderDemo on Account trigger event BeforeInsert
USER_DEBUG|[2]|DEBUG|BEFORE BEFORE_INSERT
CODE_UNIT_FINISHED|AccountOrderDemo on Account trigger event BeforeInsert
CODE_UNIT_STARTED|[EXTERNAL]|AccountOrderDemo on Account trigger event AfterInsert
USER_DEBUG|[2]|DEBUG|AFTER AFTER_INSERT
CODE_UNIT_FINISHED|AccountOrderDemo on Account trigger event AfterInsert
DML_END|[2]
USER_DEBUG|[3]|DEBUG|Saved Id: 001...This confirms several earlier points. The before trigger ran before the after trigger. Both ran inside the DML_BEGIN and DML_END pair, so the insert is one unit. The Id appeared in your code only after the DML finished, and your own after-trigger code could already use it.
Find the steps between the triggers
Validation rules, duplicate rules, flows, and workflow rules show up as their own events or code units. To see them, add them to the scenario. Create a validation rule on Account, a before-save flow, and a workflow field update, then repeat the insert. Filter the log with the search box (for example VALIDATION, FLOW, or WF_) to find each one.
A typical order for an insert in the log is:
- The before-save flow starts.
- The before trigger runs.
- Validation rules are evaluated.
- The after trigger runs.
- Workflow rules are evaluated.
- After-save flows run.
Whether you see each step depends on what exists in your org and on your debug level.
Spot a re-fire
A workflow field update that changes a field causes the before update and after update triggers to run again. In the log, this is a second CODE_UNIT_STARTED for each trigger event, now with BeforeUpdate and AfterUpdate. Look for WF_FIELD_UPDATE just before them. The second USER_DEBUG output reads BEFORE BEFORE_UPDATE, which tells you exactly where the re-entry happened.
Common errors
- No log appears. The trace flag is expired, belongs to another user, or the debug level is
NONE. - Log lines are missing. Raise the category level, or look for truncation. A log is capped at 20 MB, and older lines are removed first.
- Order looks wrong. Another trigger, flow, or managed package on the object is also running. Search for each
CODE_UNIT_STARTEDbefore concluding the platform order differs. - Too much noise. Use a narrow debug level and search for your own marker text. Prefix your
System.debugmessages with a unique tag.
Checking limits at the end
Scroll to CUMULATIVE_LIMIT_USAGE near the end. It lists SOQL queries, DML statements, and CPU time for the whole transaction, including every re-fire. If a count is higher than you expected, search backward for SOQL_EXECUTE_BEGIN or DML_BEGIN to see which step used it.
Exercise
You insert an Account that has a before trigger, an after trigger, and a workflow rule with a field update that changes a field. In the debug log you see BeforeInsert, AfterInsert, WF_FIELD_UPDATE, BeforeUpdate, and AfterUpdate code units. Explain why the update triggers appear and which log line points to the cause.
Show answer
The workflow field update changed the record after the insert was saved, so Salesforce ran the before update and after update triggers once more. The WF_FIELD_UPDATE line immediately before the BeforeUpdate code unit points to the cause. A recursion guard, such as a static set of processed Ids, would skip work in the second pass.
Exercise
You run an insert, but no debug log is created. Name two things to check.
Show answer
Check that a trace flag exists for the user running the transaction and has not expired. Also check that the debug level is not set to NONE, and that the transaction is not running as a different user.