What is the common practice in handling the response of a callout?

the response I get is a String datatype but it's value has xml elements within in them.

I was thinking of manipulating the contents manually and putting it in a map but I am not quite certain if this is the common approach.

SOAP Response: SOAP Response

Is putting this in a map the best approach?

Or is it suppose to be set in an object? (a DTO?) If so, how?

Method returning a String

public class GlobalWeatherSoap {
    public String endpoint_x = 'http://www.webservicex.net/globalweather.asmx';
    public Map<String,String> inputHttpHeaders_x;
    public Map<String,String> outputHttpHeaders_x;
    public String clientCertName_x;
    public String clientCert_x;
    public String clientCertPasswd_x;
    public Integer timeout_x;
    private String[] ns_map_type_info = new String[]{'http://www.webserviceX.NET', 'GlobalWeatherWSDL'};
    public String GetCitiesByCountry(String CountryName) {
        GlobalWeatherWSDL.GetCitiesByCountry_element request_x = new GlobalWeatherWSDL.GetCitiesByCountry_element();
        request_x.CountryName = CountryName;
        GlobalWeatherWSDL.GetCitiesByCountryResponse_element response_x;
        Map<String, GlobalWeatherWSDL.GetCitiesByCountryResponse_element> response_map_x = new Map<String, GlobalWeatherWSDL.GetCitiesByCountryResponse_element>();
        response_map_x.put('response_x', response_x);
          new String[]{endpoint_x,
        response_x = response_map_x.get('response_x');
        return response_x.GetCitiesByCountryResult;

for reference this is the wsdl that I have imported http://www.webservicex.net/globalweather.asmx?WSDL -- I removed all port related elements that are not named "GlobalWeatherSoap" this allowed me to import and generate the apex wsdl

1 Answer 1


If you use wsdl2apex, you'll get native DTO-style functionality in your Apex Code. From a developer standpoint, all you write is something like this:

try {
    MyService.Response res = MyService.getWeather(zipCode);
    // Get whatever information you want here.
} catch(Exception e) {
    // Something Bad Happened

To invoke wsdl2apex, go to Setup > Develop > Apex Classes > Import WSDL.

Not all WSDL files can be imported, though, so you may not be able to directly use this approach. If that's the case, You'll want to use Dom.Document to load your XML file, then retrieve the elements you want using the various Dom.XmlNode functions.

HttpResponse res = (new Http()).send(req);
Dom.Document result = res.getBodyDocument();
String wind = result.getRootElement().getChildElement('Wind').getText();

The Dom.Document/Dom.XmlNode interface lets you retrieve various elements quickly and efficiently, or iterate through them in a controlled manner without the need for manual parsing.

  • As what you have described in the first code block it appears that the getWeather method generated by salesforce returns an object. In my case it returns a String. Perhaps I made a mistake in modifying the elements in the wsdl file that I have imported? I had to modify the wsdl before importing it because it was initially giving me a "multiple portType" error
    – Kurusu
    Jan 12, 2015 at 6:41
  • here is the wsdl file that I have used webservicex.net/globalweather.asmx?WSDL -- I removed all of the portTypes that are not set with the name "GlobalWeatherSoap" when I did this I was able to import the wsdl thus allowing salesforce to generate the class
    – Kurusu
    Jan 12, 2015 at 6:43
  • I just replicated what you ran into. It's almost as if the response type is incorrect. I'm going to play with this a bit more and see if I can't generate a proper WSDL/class for you.
    – sfdcfox
    Jan 12, 2015 at 16:46
  • So do I need to know how to create wsdl's from scratch to get the right apex class out of it? Is there a reference you could share to help me know more about it?
    – Kurusu
    Jan 12, 2015 at 17:12
  • @Kurusu Normally, you shouldn't. However, salesforce.com only supports a proper subset of WSDL's, which can make getting a proper SOAP response non-trivial.
    – sfdcfox
    Jan 12, 2015 at 17:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .