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.
Usually the callout is a Synchronous process, it means it waits for the response and halt the process until the response comes. Whereas Continuation is a Asynchronous process, so it moves on to other process and don't wait for the response.
To implement Continuation, we need an apex class with following syntax
@AuraEnabled(continuation=true)
public static Object createContinutionObject() {
Continuation con = new Continuation(40);
// Send Http request
return con;
}
Above method will return continuation object, In this method we define, timeout, endpoint and callback method. Callback method will be called automatically once a response arrive.
con.continuationMethod = 'fetchResponse';
Here we are defining a callback method 'fetchResponse'.
Here is the sample code from documentation
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!'; // Set the State for callback function
// 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;
}
}
In the callback method, you can de-serialize the response returned from API endpoint.
The Syntax of callback method -
public static Object processResponse(List<String> labels, Object state)
using labels you can get response from API, state is the value you set in the state property in your Continuation object.
1. you can set max 3 callouts to a continuation object.
2. An Apex method that returns a Continuation object can’t perform Data Manipulation Language (DML) operations. DML statements insert, update, merge, delete, and restore data in Salesforce. If a DML operation is performed within the continuation method, the continuation execution doesn’t proceed, the transaction is rolled back, and an error is returned. You can perform DML operations in the Apex callback method for the continuation.
3. The framework processes actions containing a continuation serially from the client. The previous continuation action call must have completed before the next continuation action call is made. At any time, you can have only one continuation in progress on the client.
Comments