Newer Version Available

This content describes an older version of this product. View Latest

Work with a Continuation in an Apex Class

To work with a continuation in an Apex class, use the Apex Continuation object.
  1. Before you can call an external service, you must add the remote site to a list of authorized remote sites in the Salesforce user interface. From Setup, in the Quick Find box, enter Remote Site Settings. Select Remote Site Settings, and then click New Remote Site. Add the callout URL corresponding to LONG_RUNNING_SERVICE_URL in the Apex Class Continuation example below.

    If the callout specifies a named credential as the endpoint, you don’t need to configure remote site settings. A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. In your code, specify the named credential URL instead of the long-running service URL.

  2. To make a long-running callout, define an Apex method that returns a Continuation object. (Don’t worry about the attributes of the @AuraEnabled annotation yet. We explain that soon.)
    1@AuraEnabled(continuation=true cacheable=true)
    2public static Object startRequest() {
    3    // Create continuation. Argument is timeout in seconds.
    4    Continuation con = new Continuation(40);
    5    // more to come here
    6    return con;
    7}
  3. Set an Apex callback method to be invoked after the callout completes in the continuationMethod property of the Continuation object. In this example, the callback method is processResponse. The callback method must be in the same Apex class.
    1con.continuationMethod='processResponse';
  4. Set the endpoint for a callout by adding an HttpRequest object to the Continuation object. A single Continuation object can contain a maximum of three callouts. Each callout must have a remote site or named credential defined in Setup.
    1HttpRequest req = new HttpRequest();
    2req.setMethod('GET');
    3req.setEndpoint(LONG_RUNNING_SERVICE_URL);
    4con.addHttpRequest(req);
  5. Set data to pass to the callback method in the state property of the Continuation object. The state property has an Object type so you can pass in any data type that’s supported in Apex.
    1con.state='Hello, World!';
  6. Code the logic in the Apex callback. When all the callouts set in the Continuation object have completed, the Apex callback method, processResponse, is invoked. The callback method has two parameters that you can access.
    1public static Object processResponse(List<String> labels, Object state)
    1. labels—A list of labels, one for each request in the continuation. These labels are automatically created.
    2. state—The state that you set in the state property in your Continuation object.
  7. Get the response for each request in the continuation. For example:
    1HttpResponse response = Continuation.getResponse(labels[0]);
  8. Return the results to the JavaScript controller.

Complete Apex Class Example with Continuation

Here’s a complete Apex class that ties together all the earlier steps.

1public with sharing class SampleContinuationClass {
2    // Callout endpoint as a named credential URL
3    // or, as shown here, as the long-running service URL
4    private static final String LONG_RUNNING_SERVICE_URL =
5        '<insert your callout URL here>';
6    
7    // Action method
8    @AuraEnabled(continuation=true cacheable=true)
9    public static Object startRequest() {
10      // Create continuation. Argument is timeout in seconds.
11      Continuation con = new Continuation(40);
12      // Set callback method
13      con.continuationMethod='processResponse';
14      // Set state
15      con.state='Hello, World!';
16      // Create callout request
17      HttpRequest req = new HttpRequest();
18      req.setMethod('GET');
19      req.setEndpoint(LONG_RUNNING_SERVICE_URL);
20      // Add callout request to continuation
21      con.addHttpRequest(req);
22      // Return the continuation
23      return con;
24    }
25    
26    // Callback method
27    @AuraEnabled(cacheable=true)
28    public static Object processResponse(List<String> labels, Object state) {
29      // Get the response by using the unique label
30      HttpResponse response = Continuation.getResponse(labels[0]);
31      // Set the result variable
32      String result = response.getBody();
33      return result;
34    }
35}