[SalesForce] Named Credentials in wsdl2apex Class

I have a few questions regarding Named Credentials. I need to perform a SOAP based call out to an external system from Sales Force. I used the wsdl2apex function to generate the class for the call out. When I started integrating this class into my process I got to thinking about securely storing the username and password for the call out, which led me to discover Named Credentials. Question is, can Named Credentials be used in the auto-generated apex class? If so, could you please provide an example?

Thanks in advance

Best Answer

UPDATE for Winter `16 - Simplify Development of Web Service Callouts by Using Named Credentials


Short answer. No, it doesn't appear Named Credentials are supported by Wsdl2Apex.

I created minimal Named Credential for a public webservicex web service that doesn't require authentication (to check if it will work for the endpoint URL).

enter image description here

Then I tried to change the wsdl2Apex generated classes endpoint_x to use it.

wwwWebservicexNet.GlobalWeatherSoap ws = new wwwWebservicexNet.GlobalWeatherSoap();
ws.endpoint_x = 'callout:webservicex';

string weather = ws.GetWeather('Paris / Le Bourget', 'France');
System.debug(weather);

This gives:

System.CalloutException: IO Exception: unknown protocol: callout


In the past I've used protected custom settings within a managed package to store the credentials that are passed to a SOAP web service. As a hierarchy setting I can store Org, Profile, or User level credentials. When deployed as a managed package the credentials can only be accessed by the Apex defined within the package (this includes setting the values as well as retrieval).


Out of interest, I used the FuseIT SFDC Explorer (Disclosure: Made by my current employer) to generate a method from the WSDL that uses HttpRequest and a manual SOAP body rather than WebServiceCallout.invoke.

wwwWebservicexNet.GlobalWeatherSoap ws = new wwwWebservicexNet.GlobalWeatherSoap();
ws.endpoint_x = 'callout:webservicex';
string weather = ws.GetWeather_Http('Paris / Le Bourget', 'France');
System.debug(weather);

Success!

enter image description here

This isn't really Wsdl2Apex anymore as it is using an HttpRequest. It is however calling the SOAP based web service from Salesforce using details stored in a Named Credential.

Related Topic