Welcome to the 10th tutorial of the Salesforce Integration Tutorial series. In the previous tutorials, we mainly worked on creating custom REST APIs and performing callouts to REST web services. In this tutorial, we're mainly going to perform a callout to SOAP API.
I am going to use the calculator SOAP API for this tutorial available at:- http://www.dneonline.com/calculator.asmx. The actual web service WSDL is available here:- http://www.dneonline.com/calculator.asmx?WSDL. However, Salesforce was not able to parse that WSDL.
Blog Update: Most of the people were not able to parse WSDL because of the same error i.e. more than one binding so I am specifying how I removed that error from the WSDL I am using.
I got the below error on importing the calculator WSDL in Salesforce.
It says:- Error: Failed to parse wsdl: Found more than one wsdl:binding. WSDL with multiple binding not supported
When I had a close look at the WSDL file, I got to know that it consists of multiple WSDL binding tags as shown below:-
So, I modified it a little bit i.e. removed the second binding tag and it's references from the WSDL. You can see the changes below:-
1. Removed the second WSDL binding.
2. Removed the binding references.
And that's it. My WSDL is now ready to be parsed by Salesforce and it was parsed succesfully without any errors and warnings. You can use the modified WSDL for this tutorial which you can see below:-
You can copy and paste the above xml in a new file or save it directly from here. Once you have the WSDL file ready, it's time to import that in Salesforce and call our Calcuator API from apex to perform some of the basic arithmetic operations. Salesforce has a built-in WSDL2Apex generator and we're going to use that only.
Importing WSDL to Salesforce using WSDL2Apex
As you can see in the auto-generated code above, there are a number of inner classes inside CalculatorService class. The main class on which we need to focus on is CalculatorSoap. This class contains 4 methods inside it namely:- Divide, Add, Multiply and Subtract. All these methods are taking two integers in the parameters and returning a single integer in the response after performing the operation. Inside each method, an API callout is made in which the parameters are passed, and the response returned from the API is the result of the operation which is returned from the method.
Now, let's have a look at the below code snippet and test our API by executing it in the Anonymous Apex window in the developer console.
As you can see above, I am simply creating an instance of the inner class inside our CalculatorService class and calling all the methods by passing two inputs x and y and debugging the result using System.debug(). As you execute the above code in the anonymous apex window, you'll see the below error:-
We haven't created the remote site setting for our endpoint yet and I haven't done it previously in the tutorial on purpose as it clearly shows us that salesforce is trying to reach out to the calculator api i.e. a SOAP callout is being made when we're executing this code. To add a Remote Site Setting, you can again go to setup and search for Remote Site Settings. You'll see the below screen on clicking on Remote Site Settings under Security Controls.
Click on New Remote Site button and create an entry for our endpoint dneonline.com as given below:-
Once you've created the remote site setting, go back to the anonymous apex window in the developer console and execute the code. I am providing you the code shown on the screenshot below so that you can copy it and use easily while learning:-
When you execute the above code, you'll see the output in the debug logs as given below:-
As you can see above, all the operations are performed successfully by our calculator api and we're able to get the correct results by making a SOAP callout request to the external system.
P.S.:- In case you receive an error like:- System.CalloutException: IO Exception: input contained no data please try executing it once again and it should work as it maybe some kind of issue with the webservice or WSDL but it works fine 95% of the time.
Tired of reading or just scrolled down, don't worry, you can watch the video too.
That's all for this tutorial. If you liked it, do share it with your network and make sure to let me know your feedback in the comments down below.
Happy Trailblazing..!!
Nice Article to learn.
ReplyDeleteKeep up the good work..!!
Sure buddy. Happy to see that you liked it :-)
DeleteHi Rahul,
ReplyDeleteI wanted to know how this could be modified and changed if any wsdl file is not read by Salesforce.
Regards,
Siddhartha
Hi Siddhartha,
DeleteThanks for putting that question up. I have modified the blog with the error and the solution for the same. Please have a look.