Making an Asynchronous callout from VisualForce using the Continuation object

Recently, I’ve been working on getting familiar with the ‘Continuation’ method of making callouts from VisualForce.There are some great articles on this already, but I struggled to find any working examples that I could copy/paste into my developer org to test out.

If you are struggling to find an example, like I was, then this might help.

What we will build
This post will give you the code necessary to try out asynchronous callouts from a VisualForce page using the ‘Continuation’ concept, neatly documented and explained here.

Using the custom VisualForce page, you a callback response time in seconds, send the callout, wait for a response and then have the result displayed on the screen. This is Continuation in action.

The code here has been lifted, and refactored so it may seem familiar. We will be using the ‘Node-Count’ web service that Pat Patterson used in this JS Remoting example to return a result from a long-running callout.

Once all the code has been copied and pasted into your org, the page should look like this:

A screenshot of the VisualForce page that can be used to test asynchronous callouts using Continuation.

VisualForce page

<apex:page controller="ContinuationController" showHeader="true" tabStyle="Account" sidebar="False">
<apex:form >
<apex:pageBlock title="Asynchronous Continuation Example">
    <apex:pageBlockSection title="1. Set callout duration" columns="1">
          <apex:outputLabel value="Enter a value in seconds" for="asyncResponseTime"/>
          <apex:inputText required="true" id="asyncResponseTime" value="{!asyncResponseTime}"/>
    <apex:pageBlockSection title="2. Make Callout">
          <apex:commandButton action="{!startRequest}" value="Start callout" reRender="result" status="pageStatus"/> 
   <apex:pageBlockSection title="3. Wait for callout response">
       <apex:actionStatus id="pageStatus">
        <apex:facet name="start">
            <apex:outputPanel >
                <img alt="" title="" src="/img/loading32.gif" width="25" height="25" />
                <apex:outputLabel value="Loading..."/>

    <apex:pageBlockSection title="4. View Results from Async Callout">
      <!-- This output text component displays the callout response body. -->
      <apex:outputText id="result" value="{!result}" />

APEX Controller

public with sharing class ContinuationController {
    public String result {get; set;}
    public transient String requestLabel;
    public String asyncResponseTime {get; set;}

    public Continuation startRequest() {  
        // Make an HTTPRequest as normal
        String url = '' + asyncResponseTime;
        HttpRequest req = new HttpRequest();
        // SYNCHRONOUS - This is how we would normally make a synchronous callout
        // HttpResponse res = h.send(req);

        // ASYNCHRONOUS - Create a Continuation for the HTTPRequest, with timeout        
        Continuation con = new Continuation(60);
        con.state = con.addHttpRequest(req);
        con.continuationMethod = 'processResponse';    // define the call back method   
        requestLabel = con.addHttpRequest(req);   // returns a unique Id for this request, that will be used later
        return con; // returning a valid Continuation object indicates this VF page processing must be suspended 

    public Object processResponse() {  
       HttpResponse response = Continuation.getResponse(this.requestLabel);
       this.result = response.getBody();
       return null; // returning null indicates there is no need to suspend the VF processing any more.

Related articles and further reading

If you are interested in any further information on Continuation, then I recommend these excellent resources that I found while doing this:


Thanks for reading, any comments welcome!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *


Seo wordpress plugin by