Sub topic 4
Limits that shape integration design
This chapter assumes you know Salesforce objects and records, have called a REST API with JSON, and have seen a basic Apex class. It covers the limits that most often decide whether a native integration holds up or needs a middleware layer. The numbers below are typical values. Salesforce changes them between releases and by edition, so check the current documentation for your org before you design around a specific figure.
Why limits matter more than features
Salesforce runs many customers on shared infrastructure (it is multitenant). To keep one customer's code from starving the others, the platform enforces limits. Most native integration tools work inside those limits. A design that works with 50 records a day can fail at 50,000, even though the code has not changed.
The limits fall into three groups:
- Governor limits apply to one Apex or Flow transaction.
- API limits apply to calls made into Salesforce and are counted per org.
- Event limits apply to Platform Events and Change Data Capture, covering publishing, delivery, and retention.
Governor limits inside a transaction
A transaction is one unit of work, such as a record save with its triggers and flows. Governor limits cap what it can do. The ones that matter most for integration are in this table.
| Limit | Typical value | Why it matters for integration |
|---|---|---|
| Callouts per transaction | 100 | A loop that calls an API once per record stops after 100 records. |
| Cumulative callout time | 120 seconds | Slow external systems use up the budget. |
| Default callout timeout | 10 seconds (configurable up to 120) | A slow response raises an exception unless you raise the timeout. |
| Request or response size | 6 MB synchronous, 12 MB asynchronous | Large payloads fail and need splitting. |
| Heap size | 6 MB synchronous, 12 MB asynchronous | Parsing a large JSON body into objects can exceed it. |
| CPU time | 10 seconds synchronous, 60 seconds asynchronous | Heavy mapping logic in Apex consumes it. |
| DML statements | 150 | Writing results back record by record runs out quickly. |
Two further rules change how you structure code:
- No callout after uncommitted DML. If a transaction has inserted or updated records and has not yet committed, Apex rejects a callout with an "uncommitted work pending" error. Do the callout first, or move it to a separate asynchronous job.
- Callouts from triggers must be asynchronous. A trigger runs in the middle of a save, and the platform does not let it wait on an external system. Use a Queueable class or a Platform Event instead.
Batching inside the limits
The common fix for the callout cap is to send one request carrying many records. This only works if the external API accepts batches. The class below sends up to 200 orders in one callout and checks its own consumption.
public class OrderSyncJob implements Queueable, Database.AllowsCallouts {
private List<Id> orderIds;
public OrderSyncJob(List<Id> orderIds) {
this.orderIds = orderIds;
}
public void execute(QueueableContext ctx) {
List<Order> orders = [
SELECT Id, OrderNumber, Status, TotalAmount
FROM Order
WHERE Id IN :orderIds
];
List<Map<String, Object>> payload = new List<Map<String, Object>>();
for (Order o : orders) {
payload.add(new Map<String, Object>{
'id' => o.Id,
'number' => o.OrderNumber,
'status' => o.Status,
'total' => o.TotalAmount
});
}
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:ERP_Credential/orders/bulk');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setTimeout(60000);
req.setBody(JSON.serialize(payload));
HttpResponse res = new Http().send(req);
System.debug('Callouts used: ' + Limits.getCallouts()
+ ' of ' + Limits.getLimitCallouts());
if (res.getStatusCode() != 200) {
System.debug(LoggingLevel.ERROR, 'ERP rejected batch: ' + res.getBody());
}
}
}This pattern fixes the callout count but not everything. If the ERP returns a failure for 3 of 200 orders, the Apex code now owns partial-failure handling, retry logic, and an error log. That work is the start of what middleware does, and it is the reason the limits chapter ties into the decision chapters later.
API request limits
Calls into Salesforce, whether through REST, SOAP, Bulk, or a connector, count against a rolling 24-hour allocation. The allocation depends on your edition and the number of user licenses. Enterprise Edition, for example, starts from a base amount and adds more for each license. Add-on packs can raise it.
Points to remember:
- Every system that calls Salesforce shares the allocation. A new integration can use up the quota that an older one needs.
- Polling is expensive. An external system that asks "anything changed?" every minute spends about 1,440 calls a day even when nothing changes. Event-based designs avoid this.
- Record-by-record calls waste quota. Creating 10,000 records through 10,000 REST calls uses 10,000 requests. The Composite API groups up to 25 subrequests into one call, and Bulk API 2.0 processes large jobs as a set of batches.
- Concurrency is limited separately. Production orgs allow only a small number of long-running API requests at once (around 25 requests that run longer than 20 seconds). Slow inbound calls from an integration platform can hit this before the daily total.
- Exceeding the limit returns an error (
REQUEST_LIMIT_EXCEEDED). Every caller must handle it, usually by waiting and retrying.
The Bulk API has its own daily cap on batches. Large nightly loads that are split into many small jobs can hit it even when the plain API allocation looks fine.
Event limits
Platform Events and Change Data Capture (CDC) give you publish-subscribe without polling, but they have their own constraints:
- Publishing allocation. Each org can publish a limited number of events per hour and per day, depending on edition.
- Delivery allocation. External subscribers, such as middleware over the Pub/Sub API or CometD, share a daily delivery allowance. Salesforce-side triggers and flows are not counted against it.
- Retention window. Events are stored for a short time, typically 72 hours. A subscriber that is down longer than that loses events and must reconcile another way.
- Replay by ID. A subscriber stores the last replay ID it processed and resumes from there after a reconnect. If it does not keep the ID, it cannot recover exactly.
- Trigger batch size. An Apex trigger on a Platform Event receives events in batches of up to 2,000. If the trigger fails, the platform can retry it, so the handler must tolerate repeats.
Publishing an event is also not the same as the subscriber receiving it. EventBus.publish returns a SaveResult that tells you whether the platform accepted the event. It says nothing about whether any subscriber processed it.
How the limits combine
Limits rarely fail alone. A typical failure runs through several of them in turn:
The nodes after the callout show where native designs start to accumulate custom code: retries, logging, and recovery. Each one is possible in Apex, but each is code your team has to write, test, and maintain.
Design rules that follow from the limits
- Move integration work out of the user's save. Use Queueable Apex, Platform Events, or Change Data Capture so that a slow ERP does not slow down a user clicking Save.
- Send batches, not single records, whenever the target API supports it.
- Prefer events over polling for change notification, and store replay IDs in the subscriber.
- Budget the shared API allocation. List every system that calls Salesforce and estimate its daily request count.
- Design for repeated delivery. Retries and event replays mean the same message can arrive twice. Use an external ID and an upsert so that a repeat updates the existing record instead of creating a duplicate.
- Measure first. Estimate peak volume, payload size, and external response time before choosing a design. Limits are the main thing a volume estimate tests.
Which limits point toward middleware
A limit alone does not justify middleware. It points that way when you are working around it with growing amounts of custom code. These signals are worth watching:
- You are splitting work into chains of Queueable jobs just to stay under the callout and CPU caps.
- Several systems call Salesforce on schedules, and the API allocation is close to exhausted.
- The mapping logic is large enough to threaten CPU or heap limits in Apex.
- You need guaranteed delivery with a retention period longer than Salesforce events provide.
- Payloads regularly approach the 6 MB or 12 MB size caps.
If none of these apply, the native tools in the previous chapter probably remain enough. The decision chapters later in this tutorial weigh these signals against cost and ownership.
Exercise
An Apex trigger on Order must send each new order to an ERP system. During a data load, 500 orders are inserted in one transaction, and the ERP also has a bulk endpoint. Name two limits this design could break, and describe a design that avoids both.
Show answer
Calling the ERP once per order would exceed the 100-callouts-per-transaction limit, and a callout made directly in a trigger fails because the trigger runs mid-save, often after uncommitted DML. A safer design has the trigger enqueue a Queueable job (or publish a Platform Event) that runs after the commit. That job sends the orders in one or a few batched calls to the bulk endpoint, uses an external ID so retries do not create duplicates, and logs any per-record failures.
Exercise
A nightly sync from Salesforce to a data warehouse polls the REST API every minute for changed records. Which limit is it likely to consume, and what native alternative reduces the usage?
Show answer
It consumes the 24-hour API request allocation, since each poll counts even when nothing changed, and it shares that allocation with every other integration. Change Data Capture or Platform Events let Salesforce push changes to the subscriber, so no polling requests are made. Bulk API 2.0 is another option for the large initial or nightly extract.