0

In my requirement, I have to make multiple callouts with the Lightning component's server-side page load method. So here I have created multiple HTTP request(i.e: 20) objects and bind each 3 in different continuation instances(20/3 => 7). So in under this solution, I have to return a List from Apex to the client-side server as a response JSON. But when I do so, I received a blank object, instead of a JSON object.

Code Block here:

// Action method
@AuraEnabled(continuation=true cacheable=true)
public static List<continuation> startRequest() {
    Map<String,String> headers=new Map<String,String>();
    String access_token = 'L90NMyHS2BcLSdtQ2iyJyr1MD5WdGjEt';
    headers.put('Content-Type','application/x-www-form-urlencoded');
    headers.put('Accept','application/json');
    headers.put('Authorization','Bearer ' + access_token);
    List<String> folderList = new List<String>{'108947290168', '108950868379'};
    List<continuation> lstCon = new List<continuation>();
    // Create continuation. Argument is timeout in seconds.
    for(String objFol : folderList){
        Continuation con = new Continuation(40);
        LONG_RUNNING_SERVICE_URL = LONG_RUNNING_SERVICE_URL+'objFol';
        // Set callback method
        con.continuationMethod='processResponse';
        // Set state

        // Create callout request
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint(LONG_RUNNING_SERVICE_URL);
        system.debug('req--'+req);
        for(String ss : headers.keyset()){
            req.setHeader(ss,headers.get(ss));   
        }

        // Add callout request to continuation
        String strteemp = con.addHttpRequest(req);
        con.state = strteemp;
        lstCon.add(con);
    }
    // Return the continuations
    return lstCon;
}

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

Note: For POC purpose when I did the same flow for a single request(without iterating the for loop) with a single continuation object I got the response correctly from the @AuraEnabled apex method to the client-side(lightning controller.js).

  • To make multiple callouts, you do not need multiple continuation instances. You just need single instance of continuation and add the requests in for loop using con.addHttpRequest and you need to return the Continuation instance back, not a list. Please see an example here. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – manjit5190 Apr 4 at 19:22
  • Hey @manjit5190, thanks for your quick reply. As I have mention in my requirement i have to make multiple(40-50) callouts through lightning component's doInit function. And as per Apex Continuation Limitation A single Continuation object can contain a maximum of three callouts(HttpRequest). link So in my requirement i am in need to create multiple continuation with set of 3 each HttpRequest. – Adarsh4sfdc Apr 5 at 8:40
  • I think you need to change the solution then. For 40bto 50ncallouts I would recommend using a meddleware if these will be long running. If these are short duration callouts, you can use a batch. – manjit5190 Apr 5 at 8:42
  • Yes we may go for meddleware for this, but as per customer demands we have to find some In-house solution for this. And also we have to sync live responses from backend to frontend so we can't go for Batch/Future class too. – Adarsh4sfdc Apr 5 at 17:01
0

Late to the party, I would say ditch continuation. Its no longer needed. The only reason we used continuation is when there we had long-running callouts from UI. But from Winter 20...

Callouts Are Excluded from Long-Running Request Limit

Every org has a limit on the number of concurrent long-running Apex requests. This limit counts all requests that run for more than 5 seconds (total execution time). However, HTTP callout processing time is no longer included when calculating the 5-second limit. We pause the timer for the callout and resume it when the callout completes.

https://releasenotes.docs.salesforce.com/en-us/winter20/release-notes/rn_apex_request_limit.htm

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.