Sub topic 8
Limits that shape your design
This chapter assumes you know basic Apex, including triggers, and what an HTTP status code means. It also assumes you have read the earlier chapters on request-reply, fire-and-forget, and batch sync. Here you will see which limits push you toward one pattern or away from another. Limit values change between releases and editions, so check the current Salesforce documentation before you commit to a number.
Two families of limits
Salesforce enforces two kinds of limits that affect integrations.
- Governor limits apply to one Apex transaction. They protect the shared multitenant runtime.
- API and platform limits apply to the whole org over a time window, such as 24 hours. They cap how much traffic crosses the boundary.
Limits that hit request-reply
Request-reply holds a transaction open while it waits for another system. These limits matter most:
| Limit | Typical value | Design effect |
|---|---|---|
| Callouts per transaction | 100 | Do not call out once per record in a loop. |
| Timeout per callout | Default 10 s, maximum 120 s | Set `setTimeout` deliberately. |
| Total callout time per transaction | 120 s | Several slow calls can exhaust the budget. |
| Request or response size | 6 MB synchronous, 12 MB asynchronous | Large payloads belong in batch sync. |
| Callout after uncommitted DML | Not allowed | Order your work carefully. |
Salesforce also caps how many long-running requests can run at once in an org. If a slow external system answers in 20 seconds or more, a burst of users can hit that cap and see errors even though each transaction is within its own limits.
The last row in the table causes the most surprises. If you insert a record and then make a callout in the same transaction, Salesforce throws System.CalloutException: You have uncommitted work pending. Make the callout first, or move it to a separate transaction with a Queueable class that implements Database.AllowsCallouts.
public with sharing class InvoiceSyncJob implements Queueable, Database.AllowsCallouts {
private String invoiceId;
public InvoiceSyncJob(String invoiceId) {
this.invoiceId = invoiceId;
}
public void execute(QueueableContext ctx) {
System.debug('Callouts used: ' + Limits.getCallouts() + ' of ' + Limits.getLimitCallouts());
String status = InvoiceClient.fetchInvoiceStatus(invoiceId);
System.debug('Invoice status: ' + status);
}
}A trigger enqueues this job with System.enqueueJob(new InvoiceSyncJob(id)). The trigger's transaction commits first, and the callout runs later in its own transaction with its own limits. The cost is that the caller no longer gets the answer immediately. You have traded a synchronous pattern for an asynchronous one, so the design must tolerate the delay.
Limits that hit fire-and-forget
Publishing an event does not wait for subscribers, but it is not free.
EventBus.publishcounts against DML limits in the publishing transaction.- Orgs have hourly publish allocations and daily delivery allocations for external subscribers. The delivery allocation depends on edition and any add-on license.
- Events are retained for a limited window, 72 hours for standard-volume events. A subscriber that is offline longer than that cannot replay what it missed.
- A subscriber written as an Apex trigger runs in its own transaction, with its own governor limits. A subscriber that makes a callout per event needs the same care as request-reply code.
Limits that hit batch sync
Bulk API 2.0 avoids per-transaction limits for the data load itself, because Salesforce processes records in the background. Its limits are org-wide:
- A cap on records processed per rolling 24 hours.
- A maximum size for each uploaded file.
- Bulk jobs count against the general API allocation, though far less than one call per record would.
For a nightly upsert of 2 million rows, this is the right pattern. Calling the REST API once per row would use up the daily request allocation, and the callout limits above would apply to any Apex in the path.
Checking your usage
Do not wait for an error to learn how close you are. The REST limits resource reports remaining allocations:
GET /services/data/v60.0/limits
Authorization: Bearer <access_token>
Accept: application/jsonThe response includes DailyApiRequests, with Max and Remaining values, and entries for other allocations such as platform events and Bulk API 2.0. A scheduled job that reads this and alerts at 80% usage gives you warning before an integration stops working.
Turning limits into design rules
- Never make a callout inside a loop. Collect the work and send one request, or enqueue a job.
- Set an explicit timeout shorter than the caller's patience, and decide what happens when it fires.
- Prefer events or Bulk API when volume is high or the answer is not needed right away.
- Monitor the
limitsresource, not only error logs.
Exercise
A trigger on Opportunity inserts a related Task and then calls an external credit-check service in the same transaction. The callout fails with "You have uncommitted work pending". Give two ways to fix it.
Show answer
Make the callout before the DML so no uncommitted work exists, or move the callout into a Queueable class that implements Database.AllowsCallouts and enqueue it from the trigger. The Queueable runs in a separate transaction after the first one commits. The second option makes the call asynchronous, so the trigger cannot use the response directly.