Tuesday, June 16, 2015

Calling an external web service from Salesforce



We have seen in the last two exercise how we can call Salesforce services using Salesforce adapter and in general using a partnerlink.

In this exercise we will see how salesforce can call an external web service, this will complete the whole cycle.

One of the most important criteria for web services to be called from Salesforce is that it should be publically available.

So if you are developing a web service you have to make sure that the you are publishing it to outside world using your corporate service registry.

Since it involves a lot of steps hence for demo purpose i am using an already existing public web service.

we will try to understand how an external web service can be called from Salesforce, and the same can be applied to SOA services as well.

The public web service that we are going to use for this document is
http://www.webservicex.net/stockquote.asmx?WSDL
However there is a limitation with Salesforce that it cannot parse a WSDL document with multiple port or binding, hence we will download the WSDL and will changes in the WSDL document to remove extra port and bindings.

Hence make sure you are updating the document accordingly.I have changed the code for this exercise and updated here,

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.webserviceX.NET/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.webserviceX.NET/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET/">
<s:element name="GetQuote">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="symbol" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetQuoteResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetQuoteResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="string" nillable="true" type="s:string" />
</s:schema>
</wsdl:types>
<wsdl:message name="GetQuoteSoapIn">
<wsdl:part name="parameters" element="tns:GetQuote" />
</wsdl:message>
<wsdl:message name="GetQuoteSoapOut">
<wsdl:part name="parameters" element="tns:GetQuoteResponse" />
</wsdl:message>
<wsdl:message name="GetQuoteHttpGetIn">
<wsdl:part name="symbol" type="s:string" />
</wsdl:message>
<wsdl:message name="GetQuoteHttpGetOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>
<wsdl:message name="GetQuoteHttpPostIn">
<wsdl:part name="symbol" type="s:string" />
</wsdl:message>
<wsdl:message name="GetQuoteHttpPostOut">
<wsdl:part name="Body" element="tns:string" />
</wsdl:message>
<wsdl:portType name="StockQuoteSoap">
<wsdl:operation name="GetQuote">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get Stock quote for a company Symbol</wsdl:documentation>
<wsdl:input message="tns:GetQuoteSoapIn" />
<wsdl:output message="tns:GetQuoteSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="StockQuoteSoap" type="tns:StockQuoteSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetQuote">
<soap:operation soapAction="http://www.webserviceX.NET/GetQuote" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="StockQuote">
<wsdl:port name="StockQuoteSoap" binding="tns:StockQuoteSoap">
<soap:address location="http://www.webservicex.net/stockquote.asmx" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



Now in order to call this web service from Salesforce first we need to register the web server to the Remote Site of Salesforce.


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. In order to configure Remote site seating do the following:
Login to Salesforce.com
Go to Set up and Click on Remote Site Settings




Click on New Remote Site and Provide the information for the web service.



Save and Validate if the Web service is registered under Remote Site Setting




Now once the Remote Site setting is done and you have the WSDL file , we can go to Salesforce and create a Apex class from this WSDL.
Login to Salesforce.com
Go to Setup and Within build Navigate to DevelopApex Classes





Once you are in the APEX classes page , click on Generate from WSDL option.
Select the WSDL which you have downloaded earlier and click on parse.





Once the parsing is done, click on generate apex code to generate the APEX class for the WSDL



Say Done and then go to Apex classes and open the newly formed APEX classes for you.




Now once you have the apex class generated for the WSDL , you can test this from Developer Console. Developer console is an inbuilt tool in Salesforce that allows us to test any apex class.
In order to initiate a developer console.
Go to Apex classes and click on Developer console.




In the new console page open up the Apex class that you have recently created.
Now Go to Debug and Select Open Execute Anonymous Window as shown below




In the new Execute window type the following code to call the web service.

wwwWebservicexNet.StockQuoteSoap proxyClass = new wwwWebservicexNet.StockQuoteSoap();
String retVal = proxyClass.GetQuote('CTSH');
System.debug(retVal);





In the above mentioned code “CTSH” is Stock symbol for Cognizant Technology Solution.
This is the stub code created to call the quote service from APEX class. This passes the value ‘CTSH’ to the web service.
Now once the code is written click on Execute button to get the result.
There will be a light delay before loading the response




One the response is loaded you should get a list where you can find for CALLOUT_RESPONSE as highlighted below




Double click on the CALLOUT_RESPONSE to check out the response that is coming back from the web service.
To validate the response we will call it directly from the dot net web service validate whether the response received is correct or not.
Log in to http://www.webservicex.net/ws/WSDetails.aspx?CATID=2&WSID=9
And provide the company code as shown below and click on invoke.




Once you will invoke a new page will come up with following response.