Salesforce Continuation: new way to have asynchronous callout in Apex

As this knowledge article claims,

you can do Long-Running Callouts from a Visualforce PageLong-Running Callouts from a Visualforce Page using Continuation.

<pre><apex:page controller="ContinuationController" showChat="false" showHeader="false">
   <apex:form >
      <!-- Invokes the action method when the user clicks this button. -->
      <apex:commandButton action="{!startRequest}" 
              value="Start Request" reRender="result"/> 

   <!-- This output text component displays the callout response body. -->
   <apex:outputText id="result" value="{!result}" />
<pre>public with sharing class ContinuationController {
    // Unique label corresponding to the continuation
    public String requestLabel;
    // Result of callout
    public String result {get;set;}
    // Endpoint of long-running service
    private static final String LONG_RUNNING_SERVICE_URL = 
        '<Insert your service URL>';
   // Action method
    public Object startRequest() {
      // Create continuation with a timeout
      Continuation con = new Continuation(40);
      // Set callback method
      // Create callout request
      HttpRequest req = new HttpRequest();
      // Add callout request to continuation
      this.requestLabel = con.addHttpRequest(req);
      // Return the continuation
      return con;  
    // Callback method 
    public Object processResponse() {   
      // 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.result = response.getBody();
      // Return null to re-render the original Visualforce page
      return null;

Also more detailed documentation is here

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s