SOAP Integration in Salesforce
Consuming SOAP API
In order to consume any SOAP APIs in salesforce, we need to get the WSDL file for that API. Once we have the WSDL file we need to go to Setup->Apex Classes->Generate from WSDL.
Select the WSDL file from your local system and upload it and click the Parse WSDL button.
If we don’t have any errors in the WSDL file (like the maximum size for WSDL files is 1 MB), then apex classes will be generated for this WSDL file. Name your apex class and click the Generate Apex Code button.
Once the apex class is generated, you will see the apex class contains some methods which can be called. For example, the sample WSDL file that we used contained a testSOAP method which looks something like this in the generated apex class :
public class MySOAPDemo { public String endpoint_x = 'https://ap16.salesforce.com/services/Soap/class/MySOAPDemo'; public Map inputHttpHeaders_x; public Map outputHttpHeaders_x; public String clientCertName_x; public String clientCert_x; public String clientCertPasswd_x; public Integer timeout_x; public ExternalService.DebuggingInfo_element DebuggingInfo; public ExternalService.AllowFieldTruncationHeader_element AllowFieldTruncationHeader; public ExternalService.CallOptions_element CallOptions; public ExternalService.DebuggingHeader_element DebuggingHeader; public ExternalService.SessionHeader_element SessionHeader; private String DebuggingInfo_hns = 'DebuggingInfo=http://soap.sforce.com/schemas/class/MySOAPDemo'; private String AllowFieldTruncationHeader_hns = 'AllowFieldTruncationHeader=http://soap.sforce.com/schemas/class/MySOAPDemo'; private String CallOptions_hns = 'CallOptions=http://soap.sforce.com/schemas/class/MySOAPDemo'; private String DebuggingHeader_hns = 'DebuggingHeader=http://soap.sforce.com/schemas/class/MySOAPDemo'; private String SessionHeader_hns = 'SessionHeader=http://soap.sforce.com/schemas/class/MySOAPDemo'; private String[] ns_map_type_info = new String[]{'http://soap.sforce.com/schemas/class/MySOAPDemo', 'ExternalService'}; public String testSOAP(String str) { ExternalService.testSOAP_element request_x = new ExternalService.testSOAP_element(); request_x.str = str; ExternalService.testSOAPResponse_element response_x; Map response_map_x = new Map(); response_map_x.put('response_x', response_x); WebServiceCallout.invoke( this, request_x, response_map_x, new String[]{endpoint_x, '', 'http://soap.sforce.com/schemas/class/MySOAPDemo', 'testSOAP', 'http://soap.sforce.com/schemas/class/MySOAPDemo', 'testSOAPResponse', 'ExternalService.testSOAPResponse_element'} ); response_x = response_map_x.get('response_x'); return response_x.result; } }
Now, we can simply call this method from this class to call to the third party service. Like this for example :
public class MySoapDemoCall { public static void soapCallout() { MySOAPDemo obj = new MySOAPDemo(); String response = obj.testSOAP('SFDC'); system.debug(response); } }
Exposing SOAP API
Standard WSDL Files in Salesforce
In order to provide WSDL files used by salesforce for authentication (just like Connected Apps in REST Integration). Go to Setup-> API in your org.
You will see multiple types of WSDL files available, the most important of them are :
Enterprise WSDL
This WSDL file contains all the required information for the authentication process as well as all the metadata description from our org like objects, fields etc. Hence the size of this WSDL field will differ according to the metadata in your org.
Partner WSDL
This WSDL file contains all the necessary authentication methods but no metadata info, prefect if you just want to share an auth mechanism but not expose the metadata inside of your org.
Once you have chosen the correct WSDL file to use, you can click the Generate link next to the WSDL file and share that with the third party to use for authentication.
The WSDL will contain a login() method which will need to be called with a salesforce username and password to generate a session id.
The Webservice Keyword
If you need to expose any custom apex class via SOAP API or a WSDL file, we need to create a webservice method in our apex class.
Please note that in order to use the webservice keyword we need to have the method as global and static.
global class MySOAPDemo { webservice static String testSOAP (String str) { return 'Hello!!'; } }
This is a very simple method which just returns a ‘Hello!!’ string. Once this class is created, we will see a generate WSDL above the apex class :
Click this button to generate a WSDL file for this class which will contain the ‘testSOAP’ method like this :
<binding name="MySOAPDemoBinding" type="tns:MySOAPDemoPortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="testSOAP"> <soap:operation soapAction=""/> <input> <soap:header use="literal" part="SessionHeader" message="tns:Header"/> <soap:header use="literal" part="CallOptions" message="tns:Header"/> <soap:header use="literal" part="DebuggingHeader" message="tns:Header"/> <soap:header use="literal" part="AllowFieldTruncationHeader" message="tns:Header"/> <soap:body use="literal" parts="parameters"/> </input> <output> <soap:header use="literal" part="DebuggingInfo" message="tns:Header"/> <soap:body use="literal"/> </output> </operation> </binding>
We can share this WSDL file with the third party application and they can call this apex class directly.
Calling this method with a WSDL file will return this output :
Hello!!!