public with sharing class ContinuationClass {
// Action method
@AuraEnabled(continuation=true cacheable=true)
public static Object continuationCall() {
// Create continuation. Argument is timeout in seconds.
Continuation con = new Continuation(120);
// Set callback method
con.continuationMethod='callBackMethod';
// Set state
con.state='Hello, World!';
// Create callout request
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint(MY_ENDPOINT);
con.addHttpRequest(req);
// Method is returning null from here while calling from lightning instead of returning from the callback response
**return con;** **// Actual return**
}
// Callback method ; This method is executing properly and i m getting the //response as well in my debug
@AuraEnabled(cacheable=true)
public static Object callBackMethod(List<String> labels, Object state) {
// Get the response by using the unique label
HttpResponse response = Continuation.getResponse(labels[0]);
// Set the result variable
String result = response.getBody();
**return result;** // Expected return
}
}
-
Is your endpoint is accessible directly ? also is it under remote site setting– Avijit ChakrabortyCommented Feb 26, 2020 at 4:31
Add a comment
|
1 Answer
You will get 2 debug logs (in 2 transactions) for continuation. Consider below example:
@AuraEnabled(continuation=true cacheable=true)
public static Object startRequest() {
Continuation con = new Continuation(40);
con.continuationMethod='processResponse';
con.state='Hello, World!';
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('callout:herokuAnimals');
con.addHttpRequest(req);
System.debug('con => '+con);
return con;
}
@AuraEnabled(cacheable=true)
public static Object processResponse(List<String> labels, Object state) {
HttpResponse response = Continuation.getResponse(labels[0]);
Map<String,Object> result = new Map<String,Object>();
result.put('status', response.getStatus());
result.put('body', JSON.deserializeUntyped(response.getBody()));
result.put('origin', 'continuation callout');
System.debug('result => '+result);
return result;
}
Here, the 2 debug logs (in 2 transactions) will be:
- con => Continuation:[ContinuationMethod: processResponse, Timeout: 40]
- result => {body={animals=(majestic badger, fluffy bunny, scary bear, chicken)}, origin=continuation callout, status=null}
In any case first method should return Continuation instance but in your example, the only reason I can think of why its returning null is because the endpoint may not be correct. Check that
MY_ENDPOINT
refers to a string which starts withcallout:
- Check the endpoint mentioned above correctly in Named credentials - you do not need remote site settings if named credentials is configured
-
I got the answer. I tried to call my method from other Apex Class. Which will not call the callback method as i have trigger from apex instead of VF or Lightning. Check this for reference. developer.salesforce.com/forums/?id=9060G0000005rguQAA– AbhinayCommented Apr 16, 2020 at 9:45