Newer Version Available
Callout Limits and Limitations
The following limits and limitations apply when Apex code makes a callout to an HTTP request or a web services call. The web services call can be a SOAP API call or any external web services call.
- A single Apex transaction can make a maximum of 100 callouts to an HTTP request or an API call.
- The default timeout is 10 seconds. A custom timeout can be defined for each callout. The minimum is 1 millisecond and the maximum is 120,000 milliseconds. See the examples in the next section for how to set custom timeouts for Web services or HTTP callouts.
- The maximum cumulative timeout for callouts by a single Apex transaction is 120 seconds. This time is additive across all callouts invoked by the Apex transaction.
- You can’t make a callout when there are pending operations in the same transaction. Things that result in pending operations are DML statements, asynchronous Apex (such as future methods and batch Apex jobs), scheduled Apex, or sending email. You can make callouts before performing these types of operations.
- Pending operations can occur before mock callouts in the same transaction. See Performing DML Operations and Mock Callouts for WSDL-based callouts or Performing DML Operations and Mock Callouts for HTTP callouts.
- When the header Expect: 100-Continue is added to a callout request, a timeout occurs if a HTTP/1.1 100 Continue response isn’t returned by the external server.
Apex Callouts in Read-Only Mode
During read-only mode, Apex callouts to external services execute and aren't blocked by the system. Typically, you might execute some follow-up operations in the same transaction after receiving a response from a callout. For example, you might make a DML call to update a Salesforce record. But write operations in Salesforce, such as record updates, are blocked during read-only mode. This inconsistency in behavior in read-only mode might break your program flow and causes issues. To avoid incorrect program behavior, we recommend that you prevent making callouts in read-only mode. To check whether the org is in read-only mode, call System.getApplicationReadWriteMode().
The following example checks the return value of System.getApplicationReadWriteMode(). If the return value is equal to ApplicationReadWriteMode.READ_ONLY enum value, the org is in read-only mode and the callout is skipped. Otherwise (ApplicationReadWriteMode.DEFAULT value), the callout is performed.
1public class HttpCalloutSampleReadOnly {
2 public class MyReadOnlyException extends Exception {}
3
4 // Pass in the endpoint to be used using the string url
5 public String getCalloutResponseContents(String url) {
6
7 // Get Read-only mode status
8 ApplicationReadWriteMode mode = System.getApplicationReadWriteMode();
9 String returnValue = '';
10
11 if (mode == ApplicationReadWriteMode.READ_ONLY) {
12 // Prevent the callout
13 throw new MyReadOnlyException('Read-only mode. Skipping callouts!');
14 } else if (mode == ApplicationReadWriteMode.DEFAULT) {
15 // Instantiate a new http object
16 Http h = new Http();
17
18 // Instantiate a new HTTP request, specify the method (GET)
19 // as well as the endpoint.
20 HttpRequest req = new HttpRequest();
21 req.setEndpoint(url);
22 req.setMethod('GET');
23
24 // Send the request, and return a response
25 HttpResponse res = h.send(req);
26 returnValue = res.getBody();
27 }
28 return returnValue;
29 }
30}Your Salesforce org is in read-only mode during some Salesforce maintenance activities, such as planned site switches and instance refreshes. As part of Continuous Site Switching, your Salesforce org is switched to its ready site approximately once every six months. For more information about site switching, see Continuous Site Switching.
To test read-only mode in sandbox, contact Salesforce to enable the read-only mode test option. Once the test option is enabled, you can toggle read-only mode on and verify your apps.
Setting Callout Timeouts
1docSample.DocSamplePort stub = new docSample.DocSamplePort();
2stub.timeout_x = 2000; // timeout in millisecondsThe following is an example of setting a custom timeout for HTTP callouts:
1HttpRequest req = new HttpRequest();
2req.setTimeout(2000); // timeout in milliseconds