Sub topic 14
Testing and troubleshooting approval processes
This chapter assumes you know how an approval process is built (entry criteria, steps, approvers, actions, record locking) and that you can read basic Apex and SOQL. It covers how to test a process in the UI and in Apex, and how to find the cause when a record does not behave as expected.
Test in the UI first
Before you write Apex, run the process by hand with test users.
- Activate the process in a sandbox. Only one version of a process can be active, and an active process cannot be edited until you deactivate it.
- Create a record that meets the entry criteria.
- Submit it as an allowed initial submitter.
- Log in as each approver (or use "Log in as" from Setup) and approve or reject.
- Check the record's Approval History related list after each step.
Test every branch: approve, reject, recall, and a record that fails the entry criteria. Also test with a user who is not an approver, to confirm they cannot act on the request.
Test approvals in Apex
Apex tests can submit a record and respond to the work item. Test data is isolated, so create the record, the users, and any queue or related-user data inside the test.
@isTest
private class DiscountApprovalTest {
@isTest
static void submitAndApprove() {
Opportunity opp = new Opportunity(
Name = 'Test Opp',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 50000
);
insert opp;
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Test submission');
req.setObjectId(opp.Id);
Approval.ProcessResult submitResult = Approval.process(req);
System.assert(submitResult.isSuccess(), 'Submission should succeed');
System.assertEquals('Pending', submitResult.getInstanceStatus());
System.assert(Approval.isLocked(opp.Id), 'Record should be locked');
Id workitemId = submitResult.getNewWorkitemIds()[0];
Approval.ProcessWorkitemRequest wi = new Approval.ProcessWorkitemRequest();
wi.setWorkitemId(workitemId);
wi.setAction('Approve');
wi.setComments('Test approval');
Approval.ProcessResult approveResult = Approval.process(wi);
System.assertEquals('Approved', approveResult.getInstanceStatus());
System.assert(!Approval.isLocked(opp.Id), 'Record should be unlocked');
}
}This test assumes an active process whose entry criteria match the record, and whose approver is a user the running context can act as. Apex tests can see the org's active approval processes, so a process that is inactive or has different criteria will make the test fail.
Useful assertions:
getInstanceStatus()returnsPending,Approved,Rejected, orRemoved.Approval.isLocked(recordId)confirms locking behavior.- A SOQL query on
ProcessInstanceStepconfirms who acted and what comment they left.
Common problems and fixes
| Symptom | Likely cause | What to check |
|---|---|---|
| Submit button missing | Process inactive, entry criteria not met, or user not an allowed submitter | Process status, the record's field values, initial submitters list |
| `NO_APPLICABLE_PROCESS` error from Apex | No active process matches the record | Entry criteria and the process name passed in the request |
| "Record is locked" error on edit | A request is pending | Approval History, then unlock or recall if appropriate |
| Request goes to the wrong person | Approver field or related user is blank or points to the wrong user | The user's Manager field and the approver setup |
| No email received | Email deliverability or notification settings | Setup > Deliverability, user's email settings, spam folder |
| Field update did not happen | Action sits on the wrong event, or the field is not writable | Action on submission vs. final approval, field-level security |
| Request skipped a step | Step criteria evaluated false, with the "skip" option chosen | Step criteria and the "if criteria not met" setting |
Reading the pending state
When a request seems stuck, query the open work item. It shows who the request is waiting on.
List<ProcessInstanceWorkitem> pending = [
SELECT Id, ActorId, Actor.Name, ProcessInstance.Status, CreatedDate
FROM ProcessInstanceWorkitem
WHERE ProcessInstance.TargetObjectId = :opportunityId
];
for (ProcessInstanceWorkitem w : pending) {
System.debug('Waiting on ' + w.Actor.Name + ' since ' + w.CreatedDate);
}If the actor is a queue, a user cannot act on it until they are a queue member. If the actor is an inactive user, reassign the work item or have an administrator approve it.
Debugging tips
- Open the Debug Logs for the submitting user and look for approval and workflow entries.
- Add a
System.debugof the result afterApproval.processto see errors ingetErrors(). - Remember that after-approval field updates can fire triggers and flows. Unexpected changes often come from those, not from the process itself.
Exercise
A test calls Approval.process with a ProcessSubmitRequest and gets NOAPPLICABLEPROCESS, although the process works in the UI. Name two likely causes.
Show answer
The process may be inactive in the environment where the test runs, or the test record does not meet the entry criteria (for example, a required field value such as Amount is missing). Another cause is passing a process name that does not match the developer name of an active process.
Exercise
A request has been pending for a week and the approver says they never saw it. How do you find out who it is waiting on?
Show answer
Query ProcessInstanceWorkitem for the record's TargetObjectId and read the Actor. If the actor is a queue, check its members. If the actor is an inactive user, reassign the work item or approve it as an administrator.