Invoking Continuation Class from Lightning Component

Continuation?class in Apex is used to make a long-running request to an external Web service.

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: 

  1. labelsA list of labels, for each request the labels are automatically created. 
  1. stateThe 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 

  1. Three callouts per Continuation 

Maximum of three callouts can be made from a single continuation object.  

  1. 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. 

  1. 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

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.