Invoking Continuation class from Lightning Component

Asynchronous callouts (also referred to as Apex continuations) allow you to invoke long-running services without tying up server resources and running into Apex concurrency limits. An asynchronous callout made with a continuation doesn’t count toward the Apex limit of 10 synchronous requests that last longer than five seconds. Therefore, you can make more long-running callouts and integrate your component with a complex back-end API.

Natively salesforce didn’t allow Lightning component (Aura and LWC) to invoke Continuation apex. It was only supported from Visualforce pages. But with Summer ’19 release, an Aura component or a Lightning web component can use the Continuation class in Apex to make a long-running request to an external web service. Isn’t that good news !!!

This wonderful blog post describes still how you can invoke apex continuation from lightning component which was not yet supported natively by salesforce. This requires the usage of Visualforce page embedded as an iframe in the Lightning component. This VF page will then do the magic to invoke Continuation apex and post the response back to Lightning component.

But with the new release, you can easily invoke the Continuation apex from Lightning component. Below is the example with the details. But before starts, you need to register for the Summer 19 pre release org.

Here is the apex class that use the continuation and make the long running callout.

@AuraEnabled(continuation=true cacheable=true)

global static Object getProduct(Integer productId, Integer latency){

// Make an HTTPRequest as we normally would

// Remember to configure a Remote Site Setting for the service!

String url = ‘https://long-running.herokuapp.com/products/’ + productId + ‘?latency=’ + latency;

HttpRequest req = new HttpRequest();

req.setMethod(‘GET’);

req.setEndpoint(url);
// Create a Continuation for the HTTPRequest
//Timeout in seconds, 60 is limit
Continuation con = new Continuation(60);

con.state = con.addHttpRequest(req);

con.continuationMethod = ‘callback’;
// Return it to the system for processing

System.debug(‘returned from server’+con);

return con;

}

Use continuation=true and cacheable=true with AuraEnabled annotation to invoke it from lightning component. You have to define the call back method which process the response asynchronously.

@AuraEnabled(cacheable=true)

global static Object callback(Object state) {
HttpResponse response = Continuation.getResponse((String)state);
Integer statusCode = response.getStatusCode();
if (statusCode >= 2000) {
return ‘Continuation error: ‘ + statusCode;
}
System.debug(‘response @@’+response.getBody()+’Status code’+statusCode);
return response.getBody();
}

Use @AuraEnabled(cacheable=true) annotation with the callback method to access the response from the Lightning component.

In this example, i have used https://long-running.herokuapp.com/products/ service where you can set the latency of the transaction. So that you can experience the actual long running query behaviour.

Here is the full code of the apex class and aura component.

Please see below the video of the working example. You can see when the latency is 2000 , long running query takes longer time time than the next callout where the latency is 20.

Resource: Salesforce Release note summer 19 – Long running queries from Lightning.

One Comment

  1. Eduardo Hurtado

    Hi Paul,

    first congratulations for this post! i have a question, if you have a WSDL Object Response and not a HTTP response , how do you do? I have been looking that Continuation Object haven´t a method that ‘save’ other type httpResponse. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *