Salesforce Integration

Salesforce Apex Callouts using SOAP

Salesforce provides an easy way of making an external Web services call from your Apex and the mechanism is called Apex Callouts. Apex provides integration with Web services that either utilize SOAP and WSDL or HTTP (RESTful) services. In this article I will be using the SOAP and WSDL option to invoke a web service. As a refresher, SOAP (Simple Object Access Protocol) is a protocol specification for exchanging structured information (such as XML) securely over internet and WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information.

There are many free web services provider available on the internet but the one I like the most is from http://www.webservicex.net/ and I will be using their StockQuote web service for this tutorial. You can go to this link and give it a try – Get Quote. Put your company’s symbol and invoke the service and it should return you an XML with the stock data or some exception if it’s down. To use this particular web service, we need to download their WSDL – which can be obtained at this URL – Get Quote WSDL. Once downloaded, copy the contents starting from “<wsdl:definitions”and save into a file, let’s say “StockQuote.wsdl”.

Salesforce provides a tool called as “WSDL2Apex” which allows generating the Apex classes(and stubs) necessary for calling the web services automatically. So, instead of developing the logic to construct and parse the XML of the web service messages, the Apex classes generated by WSDL2Apex internally handles all of this overhead for developers and makes it easier to use. To import a WSDL file into Apex, go to Setup | Develop | Apex Classes, and click Generate from WSDL. Then choose the file “StockQuote.wsdl” as created earlier and click the parse button. This will fail for you as it happened to me as well. The reason being Salesforce currently does not support multiple portTypes.

I have posted an updated working version of same WSDL here in a text document as was not allowed to upload an *.wsdl, *.xml format – download and compare the contents to see what changes I made to make it working stockquote.wsdl. Repeat the step to import this updated WSDL and you should see success with no errors or warnings. On this screen, you can keep the name of the Apex class as auto-generated one based on Namespace or you can put a meaningful name – in my case, I used the name ‘SOAPStockQuoteService’ and click “Generate Apex Code”. This will generate 2 Apex classes namely – ‘SOAPStockQuoteService’ and ‘AsyncSOAPStockQuoteService‘. Both these classes are used to call the web service method synchronously and asynchronously respectively.

Remote Site Settings – Before we can make web service call, we need to setup the end point in the Remote Site Settings for the particular destination otherwise Salesforce security will not let you make calls and you will receive errors. So before we proceed further with the code, go to Administrative Setup | Security Controls | Remote Site Settings, and click ‘New Remote Site’ and put the details as shown below.

Synchronous and Asynchronous web services Call (using Continuation)

In addition to the 2 Apex classes automatically generated above, I have listed down the code I wrote in the below section with proper comments so it’s would be easier to understand.

Click here for demo

ViewStockQuote.vfp – This is a Visual Force page where you enter the company symbol and click the search button – it then displays the stock details. Change the action to point to Sync versus Async method in Custom controller.

ContinuationSOAPController.apxc – This is the Custom Controller class which is called by the VF page. The class has 2 methods for each type – sync and async call. The async call (startRequestAsync) is making use of the Continuation object and setting a callback method(processResponse()) so than when the response comes back later, it will handle and process it accordingly.

 

Test Classes –

TestContinuationSOAPController – This is the test class for both the services and provides 100% code coverage.

TestAsyncSOAPStockQuoteServiceMockImpl – Since we have used the SOAP, we have to provide a mock implementation using the WebServiceMock class as Test classes are not able to make actual Web service calls. In case of Async call, the implementation is kept blank.

 

This is the mock implementation for the synchronous test sending the proper response. You can add another block/response to test the negative scenario as well.

 

Feel Free to provide your valuable comments or point out any issues or scope of improvement in the above code.