Thursday 24 May 2018

Callouts from a Visualforce Page | Continuation | Asynchronous callouts from VF page

Most of the time got the requirement to do callout from VF page button. We can do the same with the help of Continuation method. An asynchronous callout is a callout that's made from a Visualforce page for which the response is came back through a callback methodology. An asynchronous callout is additionally referred to as a continuation.

(This Image is from Salesforce documentation)

Apex Class:-

public class AnimalsCallouts {

    // Unique label corresponding to the continuation
    public String requestLabel;
    public String result {get;set;}
    public List<Object> animals {get;set;}
    public AnimalsCallouts(){ 
        animals =new List<Object>();

    public Object  makeGetCallout() {
        Continuation con = new Continuation(40);
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        // Add callout request to continuation
        this.requestLabel = con.addHttpRequest(request);
        // Return the continuation
        return con; 
    public Object processResponse() {  
        HttpResponse response = Continuation.getResponse(this.requestLabel);

        // Set the result variable that is displayed on the Visualforce page
        if (response.getStatusCode() == 200) {
            this.result = response.getBody();
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            animals = (List<Object>) results.get('animals');
        return null;

NOTE:- Dnt forget to add Remote Site Setting for your URL.

Visual Force Page:-
<apex:page controller="AnimalsCallouts">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton action="{!makeGetCallout}" value="GetCall" reRender="detail"/>
            <apex:pageBlockSection id="detail" columns="1">
                 {!result }
                 <apex:pageBlockTable value="{!animals }" var="an">
                     <apex:column value="{!an}" headerValue="Response"/>



If you want to learn about REST API check below post


Amit Chaudhary

1 comment:

  1. Hi Amit, Can we make this Continuation callout from LWC ?