Monday, 24 February 2014

Quick hands-on tutorial of consuming external web services from SalesForce


Recently I had to perform some research of SalesForce in order to reuse some web services on SalesForce application.
Previously I had absolutely no experience of using SalesForce, so many features were unknown for me. But surprisingly the task was not so difficult.
I used freely available web service for my research:
I used this method as an example:
Add external web service URL into “Remote Site Settings” (it is important because no external web services can be used by default):
Download Weather WSDL:
Modify WSDL file: remove multiple parameters (multiple ports, bindings, services sections of WSDL file are not supported by SalesForce Apex classes):
  1. Remove extra port types:
  1. Remove extra bindings:
  1. Remove extra services:
Generate Apex class for WSDL:
Create a new controller:
public class MyController {
   public Map getElements() {
        wsCdyneComWeatherws.WeatherSoap info = new wsCdyneComWeatherws.WeatherSoap();
        info.endpoint_x = 'http://wsf.cdyne.com/WeatherWS/Weather.asmx';
        wsCdyneComWeatherws.ArrayOfWeatherDescription data = info.GetWeatherInformation();
        
        Map ret = new Map();
        
        for (wsCdyneComWeatherws.WeatherDescription item: data.WeatherDescription) {
            ret.put(item.Description, item.PictureURL);
        }
        
        return ret;
    }
}
Create a new page:

<apex:page controller="MyController"> Test page <apex:pageBlock title="Weather icons"> <table> <apex:repeat value="{!elements}" var="item"> <tr> <td>{!item}</td> <td><img src="{!elements[item]}" /></td> </tr> </apex:repeat> </table> </apex:pageBlock> </apex:page>


Now we can test the new page:
It was pretty easy, huh? :)

No comments: