Continuation object in Apex – Asynchronous callouts for long running request – Live Demo

We may run into scenario in Salesforce project, where we need call external web service but no need to wait for response. Response from external web service can be processed asynchronously and once processed it can be presented in Visualforce page.

We can accomplish this by using Actionsupport or Javascript remoting. However, this is possible with using Continuation method as well.  In Continuation approach, we can call external web service and inform that which callback method should be used to  process response. After execution of callback method, data is presented in visualforce page.

Execution Flow of an Asynchronous Callout - Image from Salesforce documentation
Execution Flow of an Asynchronous Callout – Image from Salesforce documentation

Below code demonstrate complete usage of this.

Apex code

* 	@Author	:	Jitendra Zaa
 * 	@Web	:
 * */
public class ISOtoCodeService {
    public String countryISO {get;set;}
    public String response {get;set;} 

	private String baseSericeURL = '';
    private String returnedContinuationId ;

    public ISOtoCodeService()
        countryISO = 'IN';

    public Object requestService(){

        //Timeout in seconds, 60 is limit
        Continuation con = new Continuation(60);

        // Set callback method

        // Create callout request
        HttpRequest req = new HttpRequest();

        returnedContinuationId = con.addHttpRequest(req);

        return con;

    public Object renderResponse() {
      // Get the response by using the unique label
      HttpResponse httpRes = Continuation.getResponse(returnedContinuationId);
      // Set the result variable that is displayed on the Visualforce page
      response = httpRes.getBody();
      // Return null to re-render the original Visualforce page
      return null;


Visualforce code

<apex:page tabStyle="Opportunity" controller="ISOtoCodeService" docType="html-5.0" sidebar="false" showHeader="false" showChat="false">
    <apex:form >
        <apex:pageBlock title="Continuation Demo">
            Country ISO :
            <apex:input label="Country ISO" value="{!countryISO}"/>
            <apex:commandButton action="{!requestService}" value="Request Service" reRender="responseBlock"/>


        <apex:pageBlock title="Response from Webservice" id="responseBlock">

<pre> {!response} </pre>

<style type="text/css">
         	font-size : 1.7em;


Make sure to add URL “” in remote site settings.

Live Demo :

Related posts

  • VK

    Hello I cannot see the Live demo. I have tried I.E, Chrome and Firefox. In I.E it said that the content needs to be opened in another window or tab but when I did it is taking me to SF login screen. With Chrome and Firefox, it is just blank frame. Trying to load the frame in another tab/window does not do anything. Please help.

  • dmachop

    How do you do with multiple request and responses?

  • Sanchit Dua

    What are the governor limits mitigated by the use of Continuation object?

    • Hey Sanchit,
      It will allow you to escape the limit of ten concurrent long-running callouts, where ‘long-running’ means ‘more than five seconds’.

      • Sanchit Dua


  • Ron Nelson

    I’m working on a use case where I want this to be executed on page load. I’m attempting to re-work the existing web service call into an asynchronous call. Can this be done in much the same way?

    • You should be able to do it. You can try a small example to execute this on page load.

  • Jonas Fernandez

    I tried to use your code for testing but whenever I tried to request service, I’m getting an array “Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexObjectValueELAdapter”. should I change the return type to PageReference instead of Object?

    • Rodolfo Carvalho

      It happened to me as well, and I’ve just figured it out. Make sure your Visual Force page AND Apex class API versions are at least 30. Mine was 27 and it wasn’t working.

  • Shivani

    Hi ,

    Can you provide some sample or direct towards any doc if it needs to make more than one async calls? For example – login, getSomeData, logout.