SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Saturday 4 January 2020

Salesforce Integration Tutorial Part 10 - SOAP API Callout

Hello Trailblazers,

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

In order to import WSDL to Salesforce, we're going to login into our Salesforce Org and follow the below steps:-

1. Go to Setup and search for Apex Classes and open the Apex Classes page.


2. You'll see that on the Apex Classes page, there is a button named Generate from WSDL which is used to import WSDL to Salesforce. Click on that button and you'll see the below screen.


3. Click on the Choose File button and select your WSDL file which is nothing but the same XML file that I described in the starting of the tutorial. WSDL mainly stands for Web Services Description Language. The WSDL file consits of the detailed description of SOAP Web Service which consists of the web service URL and the request/response formats for different operations that can be performed using the SOAP API. Here, I have saved the above XML in a file known as calculator.xml and have chosen that as shown below.


4. Click on the Parse WSDL button.You'll see the below screen.


5. You can change the Apex Class Name to anything you want, I am updating it to CalculatorService as shown below.

6. Click on Generate Apex Code button. It will automatically create the Apex Classes from the WSDL imported and you'll see the below screen which shows the names of two newly created apex classes in your org i.e. CalculatorService and AsyncCalculatorService:-


7. Click on Done. You'll see that two new apex classes are created in your org with the name:- CalculatorService and AsyncCalculatorService. For this tutorial we're going to focus on CalculatorService class. The code for the same is given below:-

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..!!

4 comments:

  1. Nice Article to learn.

    Keep up the good work..!!

    ReplyDelete
    Replies
    1. Sure buddy. Happy to see that you liked it :-)

      Delete
  2. Hi Rahul,

    I wanted to know how this could be modified and changed if any wsdl file is not read by Salesforce.

    Regards,
    Siddhartha

    ReplyDelete
    Replies
    1. Hi Siddhartha,

      Thanks for putting that question up. I have modified the blog with the error and the solution for the same. Please have a look.

      Delete