0
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 Chakraborty Feb 26 at 4:31
0

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:

  1. con => Continuation:[ContinuationMethod: processResponse, Timeout: 40]
  2. 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

  1. MY_ENDPOINT refers to a string which starts with callout:
  2. Check the endpoint mentioned above correctly in Named credentials - you do not need remote site settings if named credentials is configured
| 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.