Sub topic 1
Transactions and callout limits
Prerequisites
This chapter assumes you can read basic Apex, know what a trigger is, and have met governor limits before. It also assumes you know that an HTTP request has a method, headers, a body and a status code.
What a transaction is
A transaction is one unit of Apex work. It starts when something runs your code, such as a record save, a button click or a web service call. Every DML statement (insert, update, delete) in that unit is held as uncommitted work. If the code finishes without an unhandled exception, Salesforce commits everything together. If an unhandled exception occurs, Salesforce rolls everything back.
A trigger runs inside the transaction of the save that fired it. Any code the trigger calls shares that transaction and its limits.
Why uncommitted work matters for callouts
A callout is a request from Apex to a system outside Salesforce. Salesforce cannot roll back what an external system does. If your code made a DML change, then called out, and then failed, the database change would be undone but the external change would stay. The two systems would disagree.
To prevent this, Salesforce blocks a callout while the transaction holds uncommitted work. This code shows the failure:
public class CalloutAfterDml {
public static void run() {
insert new Account(Name = 'Acme');
HttpRequest req = new HttpRequest();
req.setEndpoint('https://example.com/ping');
req.setMethod('GET');
// Throws System.CalloutException: You have uncommitted work pending.
// Please commit or rollback before calling out.
new Http().send(req);
}
}Reversing the order works, because no uncommitted work exists when the callout runs:
public class CalloutBeforeDml {
public static void run() {
HttpRequest req = new HttpRequest();
req.setEndpoint('https://example.com/ping');
req.setMethod('GET');
HttpResponse res = new Http().send(req);
insert new Account(Name = 'Acme');
}
}The next chapter shows why a trigger cannot use this reordering trick.
Callout limits
Callouts also count against their own governor limits. These are the values to know:
| Limit | Value |
|---|---|
| Callouts per transaction | 100 |
| Default timeout for one callout | 10 seconds |
| Maximum timeout for one callout | 120 seconds |
| Total callout time per transaction | 120 seconds |
| Request or response size, synchronous | 6 MB |
| Request or response size, asynchronous | 12 MB |
Callout time counts toward the total even if the transaction does little else. Ten callouts that each take 15 seconds use 150 seconds, so the transaction fails before the tenth completes.
Other limits apply to the same transaction. Synchronous Apex gets 100 SOQL queries, 150 DML statements and 10,000 ms of CPU time. Asynchronous Apex gets 200 SOQL queries and 60,000 ms of CPU time. Check the current Apex Developer Guide before relying on exact numbers, because Salesforce can change them.
Key points
- A transaction commits or rolls back as one unit.
- A callout is blocked while the transaction has uncommitted work.
- Callouts have a count limit, a per-call timeout and a total time limit.
- Asynchronous Apex runs in a new transaction with higher limits, which is why the rest of this tutorial uses it.
Exercise
A method inserts a Contact and then sends an HTTP request. What error occurs, and what are two ways to avoid it?
Show answer
It throws System.CalloutException with "You have uncommitted work pending". Avoid it by making the callout before the DML statement, or by moving the callout into a separate asynchronous transaction, such as a future method or a Queueable job.
Exercise
A transaction makes 8 callouts, and each takes 20 seconds to respond. Does it succeed? Why?
Show answer
No. The count of 8 is under the limit of 100, and each 20-second call is under the 120-second per-call maximum. The total is 160 seconds, which exceeds the 120-second total callout time per transaction.