Salesforce provides a tool called Wsdl2Apex that allows you to generate Apex classes from a WSDL. These Apex classes act as a proxy for invoking the web service methods.
While functional, Wsdl2Apex has a number of limitations. Including:
- The generated Apex classes require code coverage, which needs to be created manually.
- You need to import the entire WSDL. In many cases you may only require a subset of the web methods. Reducing the number of methods cuts down the lines of Apex (a limited resource) that are generated and subsequently the number of lines requiring code coverage.
- Support for complex types that extend a base type.
<xsd:extension base="foo:Bar">
. - Support for importing another WSDL.
<xsd:import>
- Support for attributes.
<xsd:attribute>
- The ordering of the generated methods appears to be arbitrary (maybe hash based ordering internally?). At the very least, a small change in the input WSDL can produce Apex that doesn't diff very well. This can be a pain with source control and tracking the history of changes.
- Conflicts between named WSDL elements and reserved keywords in Apex. E.g. long
- WSDLs that contain multiple wsdl:binding elements
I've been working with an intern student here at FuseIT to create a replacement tool as part of the FuseIT SFDC Explorer under the new WSDL tab in the 1.0.0.47 release. The current release is aimed at having reasonable feature parity with the existing Salesforce Wsdl2Apex implmentation.
Current functionality:
- Import a WSDL from URL or local file.
- User definable class names for each WSDL namespace
- Detection of existing Apex Classes that match those being generated
- The ability to select just the Apex methods that should be generated (including a description of the input and output parameters)
- Publish the Apex classes directly into a Salesforce Org via the Tooling API.
Work in progress and possible future extensions:
- Generate test Apex methods and mocks that give 100% code coverage for the generated callout code.
- Generate HttpRequest web service calls as an alternative/backup to the
WebServiceCallout.invoke
calls. - Generate a WebServiceMock class with expanded request/response objects and doInvoke implementation.
- Generate a wrapper Apex class that will revert to the mock implementation with running in a test case context. This could also expose the end point and timeout settings as properties.
See also:
- Consuming an ASP.NET Web Service from Salesforce Apex Callouts
- Girish Arora's blog on the project
- Salesforce StackExchange Any way to easily create unit tests for a wsdl2apex generated Apex class?
- When “Generate from WSDL” fails – hand-coding web service calls
- Considerations Using WSDLs
- Wsdl2Apex - Supported WSDL Features