One of the feature we have in Salesforce is that we can easily consume External Web Services. In this article, we will learn step by step demo of consuming Web Service in Apex. There are many public websites available to consume Web Service and one of them, I am using in this article is http://www.webservicex.net , from this location we are using Stock Quote Webservice. To consume this, we need WSDL.
The Web Services Description Language (WSDL) is an XML-based interface description language that is used for describing the functionality offered by a Web Service.
WSDL have to be downloaded from this location. We need this WSDL in later part of this tutorial.
Once, WSDL is downloaded and saved on local drive. We have to go to Salesforce and navigate to “Setup | Develop | Apex Classes”. On right hand side, you will find button named as “Generate from WSDL”. This button will generate equivalent Apex class to support Webservice call. In some Programming languages, these classes are known as Proxy classes or Stubs.
When we click on “Generate from WSDL” button, it will prompt for WSDL File. Select WSDL file downloaded previously and click on “Parse WSDL” button. On next page you will get this error : “Failed to parse wsdl: Found more than one wsdl:portType. WSDL with multiple portType not supported”.
In some cases you may also get error like : “Failed to parse wsdl: Found more than one wsdl:binding. WSDL with multiple binding not supported”.
Intentionally, I have used this WSDL to explain that currently Salesforce supports only single portType and binding.
Reason, we are getting an error because “wsdltoApex” doesnt support multiple PortType, Binding, SOAP 1.2 and Schema imports. You can read more here from Salesforce documentation.
How to resolve multiple portType and Binding error in Apex while generating stubs ?
We have to modify downloaded WSDL to make sure it only contains Single Binding and single PortType. Before modifying I would suggest to read about WSDL elements. I have uploaded both versions of WSDL (Please change attached file extensions from txt to xml). First one is Actual WSDL and second I have modified to remove errors. You can compare both and check how I removed multiple PortType and Binding elements from WSDL.
- Stockquote Actual WSDL File (Change extension from txt to xml)
- Stockquote Updated WSDL File (Change extension from txt to xml)
Once, you modify your wsdl, try to generate Apex class again and this time you will be able to generate class without any error.
So, here we have successfully generated Stub classes for Webservice.
How to use generated web-service Apex stub ?
Before trying to use webservice, we have to inform Salesforce that our code will try to get some Data from External source (rather interact with external system). And therefore Remote Site Setting comes into picture.
Navigate to “Setup | Security Control | Remote Site Settings“.
Create new Site setting for URL “http://www.webservicex.net“. If we skip this step, we will endup with error like “Unauthorized Endpoint”.
We can use generated Apex class in Visualforce or some other location. To keep this tutorial simple, I am using “Developer Console”. Open Developer Console and press “Ctrl+E”.
wwwWebservicexNet.StockQuoteSoap proxyClass = new wwwWebservicexNet.StockQuoteSoap(); String retVal = proxyClass.GetQuote('CTSH'); System.debug(retVal);
In above code “CTSH” is Stock symbol for Cognizant Technology Solution. You can use any other valid Stock Symbol. Output will be visible in Console Log. We can use this output as per our need
How you will find that which Class to instantiate ?
As shown in above code, I have created Object for “wwwWebservicexNet.StockQuoteSoap”. I was able to identify that which class to instantiate? I was able to identify by porttype element name from WSDL.
How to increase Time out in Webservice ?
You may get Timeout exception while calling webservice. Default time is 10sec (At time of writing this tutorial, may change in future release). we can use “timeout_x” property to increase time to wait for response from web service. So above code can be re-written as
wwwWebservicexNet.StockQuoteSoap proxyClass = new wwwWebservicexNet.StockQuoteSoap(); proxyClass.timeout_x = 20000 ; // timeout in milliseconds String retVal = proxyClass.GetQuote('CTSH'); System.debug(retVal);
Some Known Limitations to WSDL2Apex :
- It does not support multiple port bindings.
- Inheritance is not supported in wsdl to APEX conversion.
- Complex Object types such as Enumeration are not supported.
- WSDL Import functionality is not supported.
I hope this tutorial will be helpful for newbies. Please post your comment and feedback about this tutorial. I will be delighted to answer your query about this article.