Continuation?class in Apex is used to make a long-running request to an external Web service.?This asynchronous callout does not count toward the Apex limit of 10 synchronous requests. Before Summer ’19 release Salesforce did not allow Lightning component (Aura and LWC) to invoke Continuation apex and it was only supported from Visualforce pages.
But with the new release, Continuation apex can be invoked from the Lightning component. Below is the example with details. Use continuation=true and cacheable=true with AuraEnabled annotation to invoke it from lightning component.
@AuraEnabled(continuation=true cacheable=true)
global static Object getService(){
// Remember to configure a Remote Site Setting for the service!
String endPointURL = ‘<insert your callout URL here>’;
// Make an HTTPRequest as we normally do
HttpRequest req = new HttpRequest();
req.setMethod(‘GET’);
req.setEndpoint(endPointURL);
// Create continuation and argument is timeout in seconds.
Continuation con = new Continuation (60);
// Add callout request to continuation
con.state = con.addHttpRequest(req);
// Set callback method
con.continuationMethod = ‘processResponse’;
//Set state
con.state = ‘<give Set name>‘;
return con;
}
Now, write the logic in callback method; here, we have processResponse method and this method will be invoked after all the callouts set in the Continuation object have completed. This method has two parameters:
- labels—A list of labels, for each request the labels are automatically created.
- state—The state that you set in the?state?property in your?Continuation?object.
@AuraEnabled(cacheable=true)
global static Object processResponse(List<String> labels, Object state){
// Get the response for request
HttpResponse response = Continuation.getResponse ((String)state);
// Process and return the response.
return response.getBody();
}
Continuation limits specific to usage in Aura components
- Three callouts per Continuation
Maximum of three callouts can be made from a single continuation object.
- Serial processing
Serial processing for continuation actions is allowed but only one continuation at a time. Before making the next continuation call, the previous continuation call must have completed.
- DML?not allowed
Continuation method does not allow any DML operation. If DML operation is performed within continuation then an error is returned, and the entire transaction is rolled back. We can perform DML in the Apex callback method for the continuation.
References
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/apex_continuations.htm