1

Hi I am trying to make a rest callout from controller and I am not been able to get response . I am using apex continution callout pattern for this to make call async.

This is controller class

public class UpdateAccountController {
public String accountToUpdate{get;set;}
public String status {get;set;}
public String requestLabel;

public UpdateAccountController(){
}

public Object startRequest(){
    String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm(); 
    String queryParam = 'SELECT+Id,Name+FROM+Account+WHERE+Name='+'\''+'Test'+'\''+'LIMIT+1';
    String restAPIURL = sfdcURL + '/services/data/v44.0/query/?q=' +queryParam;
    System.debug('In start Request');
    // create a new account here 
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setHeader('Accept', 'application/json');
    req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionID());
    req.setEndpoint(restAPIURL);

    // Add callout request to continuation 120 is max limit 2 mins
    Continuation con = new Continuation(120);
    // Set callback method
    con.continuationMethod='processResponse';
    this.requestLabel = con.addHttpRequest(req);        
    return con;
}

public Object processResponse() {   
  System.debug('In process request');
  // Get the response by using the unique label
  HttpResponse response = Continuation.getResponse(this.requestLabel);
  // Set the result variable that is displayed on the Visualforce page
  this.status = response.getBody();
  // Return null to re-render the original Visualforce page
  return null;
}

}

and the page

<apex:page docType="html-5.0" controller="UpdateAccountController" showChat="false" showHeader="false">
     <apex:form >
          Name <b/>
          <apex:inputText value="{!accountToUpdate}"/>
          <apex:actionStatus id="AddStatus">
               <apex:facet name="start">    
                    <apex:outputText value="Loading"/>
               </apex:facet>    
               <apex:facet name="stop"> 
                    <apex:commandButton action="{!startRequest}" value="Create Account" 
                                        status="AddStatus" reRender="status"/>
               </apex:facet>    
          </apex:actionStatus>
     </apex:form>
     <apex:outputText id="status" value="{!status}"/>
</apex:page>

sending the same request from Sync HttpCallout is working but with this pattern it get stucked.

  • While calling external webservice url its working fine but while calling salesforce internal rest service url its get stucked in loop and no response, Its there is any limitation in Continuition pattern that only external callouts are supported – Hussain Gadisaaz Feb 4 at 9:46
  • Why would you need to invoke a rest api to the same instance to fetch data, when you could directly access it right within the controller? Are you doing this just for learning or is there a specific use case here? – Jayant Das Feb 4 at 12:15
  • There is a specific use case which I can't describe here. I just want why this is not working as I am getting response for this request with Synchronous callout in apex but in Continuition its not working. – Hussain Gadisaaz Feb 5 at 7:15

Your Answer

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

Browse other questions tagged or ask your own question.