Continuation Apex Class

Use the Continuation class to make callouts asynchronously to a SOAP or REST Web service. The timeout maximum is 120 seconds.
If the continuationMethod property is not set for a Continuation, the same action method that made the asynchronous callout is called again when the callout response returns.

The following are methods for Continuation.

  • addHttpRequest(request)
    Adds the HTTP request for the callout that is associated with this continuation. You can add up tothree requests to a continuation. The timeout that is set in each passed-in request is ignored. Only the global timeout maximum of 120 seconds applies for a continuation.
  • getRequests()
    Returns all labels and requests that are associated with this continuation as key-value pairs.
  • getResponse(requestLabel)
    Returns the response for the request that corresponds to the specified label. The status code is returned in the HttpResponse object and can be obtained by calling getStatusCode() on the response. A status code of 200 indicates that the request was successful.
//This example shows how to save state information for a continuation 
in a controller.
// Declare inner class to hold state info
private class StateInfo {
String msg { get; set; }
List<String> urls { get; set; }
StateInfo(String msg, List<String> urls) {
this.msg = msg;
this.urls = urls;
}
}

// Then in the action method, set state for the continuation
continuationInstance.state = new StateInfo('Some state data', urls);

Use the Continuation class in Apex to make a long-running request to an external Web service. Process the response in a callback method. An asynchronous callout made with a continuation doesn’t count toward the Apex limit of 10 synchronous requests that last longer than five seconds.

public with sharing class SampleContinuationClass {
// Callout endpoint as a named credential URL
// or, as shown here, as the long-running service URL
private static final String LONG_RUNNING_SERVICE_URL =
'<insert your callout URL here>';

// Action method
@AuraEnabled(continuation=true cacheable=true)
public static Object startRequest() {
// Create continuation. Argument is timeout in seconds.
Continuation con = new Continuation(40);
// Set callback method
con.continuationMethod='processResponse';
// Set state
con.state='Hello, World!';
// Create callout request
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(LONG_RUNNING_SERVICE_URL);
// Add callout request to continuation
con.addHttpRequest(req);
// Return the continuation
return con;
}

// Callback method
@AuraEnabled(cacheable=true)
public static Object processResponse(List<String> labels, Object state) {
// Get the response by using the unique label
HttpResponse response = Continuation.getResponse(labels[0]);
// Set the result variable
String result = response.getBody();
return result;
}
}

@AuraEnabled(continuation=true)
An Apex controller method that returns a continuation must be annotated with @AuraEnabled(continuation=true).
@AuraEnabled(continuation=true cacheable=true)
To cache the result of a continuation action, set cacheable=true on the annotation for the Apex callback method.

There’s a space, not a comma, between continuation=true cacheable=true.

It’s best practice to set cacheable=true on all methods involved in the continuation chain, including the method that returns a Continuation object. The cacheable=true setting is available for API version 44.0 and higher.

Published by

Suhas Rathod

I'm Suhas Rathod. I am an energetic, self-confident and experienced Full Stack Programmer with a passion for working with different technology.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.