Callout for imported wsdl using continuation after page load

Hi friends,

Today I am going to provide steps to implement Callout for imported wsdl with continuation after page load.

Let’s understand why use an asynchronous call? An asynchronous callout is a callout that is made from a Visualforce page for which the response is returned through a callback method. An asynchronous callout is also referred to as a continuation.

Sometimes we have to load data asynchronously and refresh particular section after loading the page so that user should not wait longer to load other contents of the page. To achieve such kind of requirement, developer can use apex:function and make asynchronous call to web service in controller. Once webservice return the response, developer can reRender the particular section to show data received from webservice.

Following example shows that invoke controller method on document ready for asynchronous callout for imported wsdl.

 

Step 1 –

This visual force page contains a button that invokes the startAsyncCallOnLoad method of the controller that’s associated with this page. After the continuation result is returned and the callback method is invoked, the apex:function renders the apex:outputpanel component again to display the body of the response.

<apex:page controller=”ContinuationController”>

<script type=”text/javascript” src=”//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script>

<apex:form >

<apex:actionFunction name=”getDetails” action=”{!getData}” reRender=”activitytable”/>

<apex:outputPanel id=”activitytable”>

<table>

<apex:repeat value=”{!lstData}” var=”wrpAct”>

<tr><td> <apex:outputText value=”{!wrpAct.stockdata}” />

</td></tr>

</apex:repeat>

</table>

</apex:outputpanel>

 

</apex:form>

<script>

$(document).ready(

function() {

getDetails();

});

</script>

</apex:page>

 

Step 2 –

public class ContinuationController {

AsyncSOAPStockQuoteService.GetStockQuoteResponse_elementFuture stockQuoteFuture;

List<stockdata> lstData {get;set;}

Public ContinuationController(){

lstData = new List<stockdata>();

}

public Continuation getData(){

Integer TIMEOUT_INT_SECS = 60;

Continuation cont = new Continuation(TIMEOUT_INT_SECS);

cont.continuationMethod = ‘processResponse’;

// Service 1, this is just an example not real service

AsyncSOAPStockQuoteService.AsyncStockQuoteServiceSoap

stockQuoteService = new AsyncSOAPStockQuoteService.AsyncStockQuoteServiceSoap();

stockQuoteFuture = stockQuoteService.beginStockQuote(cont,’CRM’);

}

 

 

 

// Callback method

public Object processResponse() {

stockdata obj1 = new stockdata();

obj1.stockdata = stockQuoteFuture.getValue();

lstData.add(obj2);

// Return null to re-render the original Visualforce page

return null;

}

public class stockdata(){

public string stockdata {get;set;}

}

}


Comments

One response to “Callout for imported wsdl using continuation after page load”

  1. Nice stuff Sandip !

Leave a Reply

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