Sub topic 12
Monitoring and operating integrations
This chapter assumes you know the three patterns (request-reply, fire-and-forget, batch sync), Named Credentials, external IDs, and basic Apex. It covers how to see what your integrations are doing in production and how to respond when they misbehave.
Why operations needs its own design
An integration that works in a sandbox can still fail quietly in production. A callout may time out, a platform event subscriber may fall behind, or a Bulk API job may finish with some rows rejected. Nobody sees these failures unless you built a way to see them. Plan for three things: visibility, alerting, and a repeatable recovery procedure.
What to watch for each pattern
Each pattern fails differently, so each needs different signals.
| Pattern | Failure signal | Where to look |
|---|---|---|
| Request-reply | Non-2xx status, timeouts, callout limit errors | Apex logs, your own log records, Event Monitoring |
| Fire-and-forget | Publish failures, subscriber lag, trigger errors | Publish result, Platform Event usage, subscriber logs |
| Batch sync | Failed or unprocessed records, jobs stuck in progress | Bulk Data Load Jobs page, job results endpoints |
Track limits before they block you
Salesforce limits API requests over a rolling 24-hour period and limits platform event delivery and publishing. Check consumption regularly instead of discovering the limit when calls start failing.
- System Overview in Setup shows API usage against your org's allocation.
- Limits REST resource returns remaining daily API requests and other limits as JSON.
- Apex's
Limitsclass shows per-transaction use, such as callouts made so far.
GET /services/data/v60.0/limits
Authorization: Bearer <access_token>The response includes entries such as DailyApiRequests with Max and Remaining values. Poll it from your monitoring tool and alert when Remaining falls below a threshold you choose, for example 20 percent of Max.
Log integration outcomes yourself
Debug logs expire and are hard to search. For anything important, write a durable record of each integration attempt. A small custom object such as Integration_Log__c works well.
public with sharing class IntegrationLogger {
public static void log(String source, String correlationId,
String status, String detail) {
Integration_Log__c entry = new Integration_Log__c(
Source__c = source,
Correlation_Id__c = correlationId,
Status__c = status,
Detail__c = detail == null ? null : detail.left(2000)
);
Database.SaveResult result = Database.insert(entry, false);
if (!result.isSuccess()) {
System.debug(LoggingLevel.ERROR,
'Could not write log: ' + result.getErrors()[0].getMessage());
}
}
}Two details matter here. The Database.insert(entry, false) call uses partial-success mode, so a logging problem does not roll back or break the business transaction. The Correlation_Id__c field holds an identifier you also send to the external system, for example in a header. With it, you can follow one business event across both systems.
Never write secrets, tokens, or personal data into the log. Truncate response bodies, as the example does with left(2000).
Alerting
Logs only help if someone reads them. Pick a small set of alerts tied to action:
- Callout failure rate above a threshold over a window.
- Any Bulk API job that ends with failed records.
- Remaining daily API requests below your threshold.
- Records in your log object stuck in an error status for more than a set time.
Common delivery options are a scheduled Apex job or a Flow that emails a queue, a platform event consumed by your incident tool, or an external monitoring platform that polls the Limits resource and your log object.
The operating cycle
Replay is only safe because of the earlier chapters on idempotency. An upsert by external ID can be repeated without creating duplicates. Platform events can be replayed from a stored replay ID within the retention window, so a subscriber that was down can catch up. Check the current retention period in the Salesforce documentation, because it depends on the event type and edition.
Operational habits
- Keep a runbook for each integration: what it does, who owns it, how to check health, how to replay.
- Monitor credential expiry. Expired certificates, rotated secrets, and disabled integration users are frequent causes of sudden failures. Named Credentials keep the secret in one place, so you only update it there.
- Use a dedicated integration user with minimal permissions, so audit trails show which traffic came from which integration.
- Test failure paths in a sandbox. Force a timeout and a bad payload, then confirm that your alerts fire.
Exercise
A nightly Bulk API 2.0 upsert job shows state JobComplete, yet some Accounts are missing in Salesforce. Which checks and actions would you take, and why is rerunning the whole job safe?
Show answer
JobComplete means the job finished processing, not that every row succeeded. Retrieve the failed results and unprocessed records for the job, read the error messages, correct the data or the mapping, and upload only the failed rows in a new job. Rerunning is safe because the upsert uses an external ID, so rows that already succeeded update the same records and do not create duplicates. An alert on any job with failed records would have flagged this the same night.