Sub topic 18
Testing, debugging and monitoring your automation
This chapter assumes you know how record-triggered flows and Apex triggers work. It also assumes you know the order of execution and that several automations can share one object. Choosing the right tool is only half the job. You also need to prove it works, find out why it fails, and notice when it breaks in production.
Test in a sandbox first
Build and test in a sandbox, not in production. Use test data that matches real conditions: records with and without optional values, different record types, and users with different profiles. Automation often works for an admin and fails for a standard user because of field-level security or sharing.
Testing flows
Flow Builder has a Debug button. Run it with Roll back mode turned on. The flow then executes against real records, and Salesforce undoes the changes afterward. The debug output shows each element that ran, the variable values at that point, and the decision outcome.
Record-triggered flows can also have flow tests. A flow test sets up a record, runs the flow, and checks the result with assertions. Run these tests again after every change.
Add a fault path to every element that creates, updates or deletes data, or calls an action. Without one, the flow fails and the user sees a generic error. A fault path can show a clear message or notify an admin.
Testing Apex
Apex deployed to production needs at least 75% code coverage. Coverage only shows that lines ran, so write assertions too. Always test with 200 records, because a trigger processes up to 200 records per batch and a bulk-unsafe trigger fails at that size.
@IsTest
private class AccountTriggerTest {
@IsTest
static void setsHotRatingInBulk() {
List<Account> accounts = new List<Account>();
for (Integer i = 0; i < 200; i++) {
accounts.add(new Account(Name = 'Test ' + i, AnnualRevenue = 2000000));
}
Test.startTest();
insert accounts;
Test.stopTest();
Integer hotCount = [SELECT COUNT() FROM Account WHERE Rating = 'Hot' AND Id IN :accounts];
System.assertEquals(200, hotCount, 'Every high-revenue account should be Hot');
}
}Test.stopTest() also forces queued asynchronous work, such as a Queueable job, to finish before your assertions run.
Debugging in practice
- Debug logs: In Setup, add a trace flag for the user. The log shows which flows, triggers and validation rules ran, in order, and how much of each governor limit was used.
- Flow error emails: Salesforce emails the person who last modified a flow when a run fails. Check that this address is monitored.
- Order of execution: If a field has an unexpected value, a later automation may have overwritten it. Compare the debug log against the order of execution.
Monitoring in production
| What to watch | Where to look |
|---|---|
| Paused or failed flow runs | Paused and Failed Flow Interviews in Setup |
| Queueable, batch and scheduled Apex | Apex Jobs and Scheduled Jobs in Setup |
| Unexpected configuration changes | Setup Audit Trail |
| Limit errors in Apex | Debug logs and exception emails |
Exercise
A record-triggered flow updates related Contacts. It works in your sandbox but fails for a sales user in production, and the user only sees a generic error. Name two changes or checks that help you find and handle the cause.
Show answer
Add a fault path to the Update Records element so the failure shows a clear message or notifies an admin. Then reproduce the problem as that user, using a debug log with a trace flag. The cause is often field-level security or sharing that the admin account does not hit. Testing with a standard-user profile in the sandbox would have caught it.