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).
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 '20 at 19:22