+ Start a Discussion
ADARSH SINGH 17ADARSH SINGH 17 

Getting Blank response when i return Array on continuation in Lightning component's Server side method

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<Continuation> 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.
 
// 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). 
Ayush ShuklaAyush Shukla
Coninuation class does not support JSON serialization 
System.JSONException: Type unsupported in JSON: common.apex.api.continuation.Continuation
There  may be some special handling by Salesforce when we return only one instance of continuation object from server method. If you inspect the json response coming to UI controler, It returns web service related details (not the fields from continuation object)
  • args, user-agent, headers, url and others.
You can implement a workaround by creating a wrapper class like ContinuationWrapper . Store only those details in wrapper which you need in UI controller and send list of the ContinuationWrapper in server response instead of list of Continuation objects
 
// this is your inner class     
     public class ContinuationWrapper {
        @AuraEnabled
        public String continuationMethod;
        // add other fields you need in UI controller
        
        public ContinuationWrapper(Continuation con) {
            this.continuationMethod = con.continuationMethod;
            // populate other fields
        }
    }

    @AuraEnabled(continuation=true cacheable=true)
    public static List<ContinuationWrapper> 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<ContinuationWrapper> lstCon = new List<ContinuationWrapper>();
        // 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(new ContinuationWrapper(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;
    }