Sub topic 13
Troubleshooting common errors
This chapter assumes you can build a Named Credential, an External Credential, a principal and a permission set, and that you have written a callout: request in Apex. It lists the failures you are most likely to meet and how to find the cause of each.
Work from the outside in
A callout passes through several layers. Check them in order, because an error at an early layer hides every later one.
Errors before the request leaves Salesforce
These errors come from Salesforce itself, so the external system never sees the request.
| Symptom | Likely cause | Fix |
|---|---|---|
| Named credential not found, or an invalid endpoint | The name after `callout:` does not match the Named Credential API name. Names are case-sensitive in practice, so copy them exactly. | Correct the name, or deploy the Named Credential to this org. |
| Insufficient access or a missing credential error | The running user's permission set lacks External Credential Principal Access. | Add the principal to a permission set and assign it to the user. |
| Callout from a trigger or before a DML statement fails | Salesforce blocks a callout when uncommitted work is pending. | Move the callout to a `@future(callout=true)` method or a Queueable class that implements `Database.AllowsCallouts`. |
| Test fails with "Methods defined as TestMethod do not support Web service callouts" | The test has no mock. | Call `Test.setMock` before the callout. |
Remember who the running user is. A scheduled job or a Queueable class runs as a specific user, and that user needs the permission set, not you.
Errors from the external service
When the response arrives, the status code points to the next place to look.
- 401 Unauthorized: The credential is wrong or expired. Check the API key or the OAuth token. For OAuth, re-authenticate the principal.
- 403 Forbidden: The credentials are valid but the account lacks rights for this resource. Check scopes and the external account's roles.
- 404 Not Found: The path is wrong. The Named Credential URL and your path are joined, so a trailing slash in one and a leading slash in the other can produce a different URL than you expect.
- Redirects (301, 302): Apex does not always follow them as a browser does. Point the URL at the final address.
Check what you actually sent
Log the request and response while you debug. Never log secrets.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Weather_API/forecast?city=Oslo');
req.setMethod('GET');
HttpResponse res = new Http().send(req);
System.debug(LoggingLevel.ERROR, 'Status: ' + res.getStatusCode());
System.debug(LoggingLevel.ERROR, 'Body: ' + res.getBody());Credential values injected by the Named Credential are added after Apex builds the request, so they will not appear in the debug log. That is expected. If a header seems missing, check the custom header formula, such as {!$Credential.Weather_EC.ApiKey}, and confirm the parameter name matches the External Credential exactly.
Use a debug log set to the running user to see callout events. Use CalloutException handling to catch timeouts, and raise the timeout with req.setTimeout(120000) if the service is slow. The maximum is 120 seconds.
A quick checklist
- Does the name after
callout:match the Named Credential? - Is the External Credential linked to the Named Credential?
- Does the running user have a permission set with the principal?
- For per user principals, has this user authenticated?
- Does the status code and body from the service explain the rest?
Exercise
A callout works for you in a sandbox but fails for a colleague with an access error on the credential. The Named Credential and the Apex code are the same for both of you. What do you check first, and why?
Show answer
Check whether your colleague has a permission set with External Credential Principal Access for the principal. The credential configuration is shared, but access is granted per user through permission sets, so you may have the permission set and your colleague may not. If the principal is per user, also check that your colleague has authenticated to the external system.