function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deepak Joshi 21Deepak Joshi 21 

Asynchronous callout to process json data returned from asp.net web service

I want to show data returned from an asp.net web service in a visualforce page using asynchronous callout.
I have done following things to achieve the above
1. Created an asp.net web service to return data from SQL Server table in json format.
2. Generated wsdl file from the web service.
3. Parsed wsdl to generate an apex class.
4. Created a controller apex class to call the methods from class generated from wsdl.
5. Designed a visualforce page to show the output.

Currently I am calling the method synchronously from controller which is showing the result but it is causing a page postback, taking some time to load the results, showing Request Time Out Exception at times.

So I want to call the method asynchronously now.
Here is my method.
public class AsyncGetCustomer {
            public class GetCustomerJsonResponse_elementFuture extends System.WebServiceCalloutFuture {
        public String getValue() {
            getCustomer.GetCustomerJsonResponse_element response = (getCustomer.GetCustomerJsonResponse_element)System.WebServiceCallout.endInvoke(this);
            return response.GetCustomerJsonResult;
        }
    }

    public class AsyncGetCustomerSoap {
        public String endpoint_x = 'ServiceLink';
        public Map<String,String> inputHttpHeaders_x;
        public String clientCertName_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'Url', 'ServiceName'};

        public AsyncGetCustomer.GetCustomerJsonResponse_elementFuture beginGetCustomerJson(System.Continuation continuation) {
            getCustomer.GetCustomerJson_element request_x = new getCustomer.GetCustomerJson_element();
            return (AsyncGetCustomer.GetCustomerJsonResponse_elementFuture) System.WebServiceCallout.beginInvoke(
              this,
              request_x,
              AsyncGetCustomer.GetCustomerJsonResponse_elementFuture.class,
              continuation,
              new String[]{endpoint_x,
              'http://uniweb.klinkjewellery.com/GetCustomerJson',
              'http://uniweb.klinkjewellery.com/',
              'GetCustomerJson',
              'http://uniweb.klinkjewellery.com/',
              'GetCustomerJsonResponse',
              'getCustomer.GetCustomerJsonResponse_element'}
            );
        }
}

Here is my controller class
public with sharing class ctrlAsynchCustomer {
    AsyncGetCustomer.GetCustomerJsonResponse_elementFuture custFuture;
    public String result {get; set;}
   
    public Continuation startRequest(){
        Integer TIMEOUT_INT_SECS = 60;
        Continuation cont = new Continuation(TIMEOUT_INT_SECS);
        cont.continuationMethod = 'processResponse';
        AsyncGetCustomer.AsyncGetCustomerSoap custService = new AsyncGetCustomer.AsyncGetCustomerSoap();
        custFuture = custService.beginGetCustomerJson(cont);
        return cont;
    }     
        
    //Callback method
    public void processResponse(){
        result = String.valueOf(custFuture.getValue());
        // Return null to re-render the original Visualforce page
       return null;
    }
}

Here is my visualforce page
<apex:page controller="ctrlAsynchCustomer" tabStyle="Customer__tab">
    <apex:form >
        <!-- Invokes the action method when the user clicks this button. -->
        <apex:commandButton action="{!startRequest}" Value="Start Request" rerender="result"/>
    </apex:form>
    <!-- This output text component displays the callout response body. -->
    <apex:outputText id="result" value="{!result}"></apex:outputText>
</apex:page>

Now whenever I click the button the following error is shown.

Visualforce Error
Help for this Page
System.CalloutException: Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found 'http://www.w3.org/1999/xhtml:html'
Error is in expression '{!processResponse}' in component <apex:commandButton> in page asynchcustomer: Class.AsyncGetCustomer.GetCustomerJsonResponse_elementFuture.getValue: line 30, column 1
Class.ctrlAsynchCustomer.processResponse: line 28, column 1
Class.AsyncGetCustomer.GetCustomerJsonResponse_elementFuture.getValue: line 30, column 1
Class.ctrlAsynchCustomer.processResponse: line 28, column 1

When i checked the developer console for the logs of the operation i found 2 logs out of which one was successful and the other was not, following is the debug result of the successful log.

USER_DEBUG [EXTERNAL]|DEBUG|Continuation Controllers State: 485 out of 81920 bytes used (0.01%), count 1

here is the exception shown at unsuccessful log.

EXCEPTION_THROWN [30]|System.CalloutException: Web service callout failed: Unexpected element. Parser was expecting element 'http://schemas.xmlsoap.org/soap/envelope/:Envelope' but found 'http://www.w3.org/1999/xhtml:html'
 

 

Akshay Deshmukh1Akshay Deshmukh1
Hi,
Are you fetching response from Continuation object? I did not find this in your code - 'HttpResponse response = Continuation.getResponse(this.requestLabel);'. Let me know if I am missing something.

Thx
Deepak Joshi 21Deepak Joshi 21
Hi Dattaraj, Thanks for your reply, I am fetching response from an asp.net web service which is published on a web server, then I am generating wsdl from that web service and converting the wsdl to an apex class. This is what my apex class method looks like, public AsyncGetCustomer.GetCustomerJsonResponse_elementFuture beginGetCustomerJson(System.Continuation continuation) { getCustomer.GetCustomerJson_element request_x = new getCustomer.GetCustomerJson_element(); return (AsyncGetCustomer.GetCustomerJsonResponse_elementFuture) System.WebServiceCallout.beginInvoke( this, request_x, AsyncGetCustomer.GetCustomerJsonResponse_elementFuture.class, continuation, new String[]{endpoint_x, 'http://uniweb.klinkjewellery.com/GetCustomerJson', 'http://uniweb.klinkjewellery.com/', 'GetCustomerJson', 'http://uniweb.klinkjewellery.com/', 'GetCustomerJsonResponse', 'getCustomer.GetCustomerJsonResponse_element'} ); } Now I want to call this method asynchronously from my controller class. Thanks & Regards Deepak M Joshi