To work with a continuation in an Apex class, use the Apex Continuation object.
Prerequisites
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.
Note
Use the Continuation Object
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 here6 return con;7}
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';
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);
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!';
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.
Return the results to the component’s JavaScript file.
Example: Apex Class 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 URL3 // or, as shown here, as the long-running service URL4 private static final String LONG_RUNNING_SERVICE_URL =5 '<insert your callout URL here>';67 // Action method8 @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 method13 con.continuationMethod='processResponse';14 // Set state15 con.state='Hello, World!';16 // Create callout request17 HttpRequest req = new HttpRequest();18 req.setMethod('GET');19 req.setEndpoint(LONG_RUNNING_SERVICE_URL);20 // Add callout request to continuation21 con.addHttpRequest(req);22 // Return the continuation23 return con;24}2526 // Callback method27 @AuraEnabled(cacheable=true)28 public static Object processResponse(List<String>labels, Object state){29 // Get the response by using the unique label30 HttpResponse response = Continuation.getResponse(labels[0]);31 // Set the result variable32 String result = response.getBody();33 return result;34}35}