How do I Create a SOAP Webservice in Salesforce? (Tutorial 12)
Enroll in the Course: courses.mytutorialrack.com/p/salesforce-integration-with-external-systems
Hello guys welcome back so in this tutorial we are going to create an epics class and we will expose it as in soap service and we are going to call this soap service from postman.
So first thing is: you have to create the apex class? So let's go to the setup here and inside of the setup in the quick, find write in apex and then it'll go ahead and pull up this apex classes under the custom code.
So click on this apex classes and now you're going to create a new apex class.
So let's go ahead and click on the word new here and then you're going to write that apex class.
So the first thing what you're going to do is we are going to call this class with the keyword global.
If you remember the rules that we discussed in the previous tutorial, you have to use the global keyword with the class and then you also with the method that you want to expose.
You have to use the uh keywords like web service and make the method as static.
So then the next thing is global.
Then we are going to say class and then let's say we call this as student soap service.
This is the name of the class and then inside of this class, I'm going to define a method and we will call this method with the keyword web service, so we're going to say web service.
This is the keyword that you have to use and the method needs to be static and we will.
This method is going to return a student record okay.
So this is going to return a student record and then the name of the method, we'll call it as get student and this method takes in one parameter of the type string and we will go ahead and pass in the id of that student okay.
So this is going to be the id and based on that id, it is going to return you the student record, so this method takes in one parameter of the type string and which is basically the student id, and then this method returns a student record now inside of this method, all we are doing is, we are doing in select query select and we are going to query the id field and then let's say we also wanted to know.
What's the name of the student, that is another thing I wanted to find and then I also want to know the phone number of the student.
So I'm going to say phone number underscore underscore c.
Then I also want to know which school is he studying? Okay, so this is going to be, the school will be returned the id of the school, and if you wanted to capture any other information, let's say I wanted to also capture the email, so we'll go ahead and say email from what is the object name from the student underscore underscore c.
This is the custom object that we have created where, where id is equals to whatever you have provided in the call the student id, so this is going to be the select query I have written now.
This select query is going to return me a student record, so I'm going to capture the result of this query inside of this variable called student and then all I'm going to do is I'm going to in the next line.
I will just return this student record back.
That's all I'm going to do I'm going to return the student record now this is what we have done.
We have created a student soap service inside of the student soap service.
We have defined a method called get student.
Now this method takes in one parameter of the type string and we are passing in the id of the student and then inside of that method.
All I'm doing is I'm doing a simple query and I'm fetching the information of that student.
I'm fetching the student name and the id of the student, the phone number school he's enrolled in and the email address where the id matches whatever you have provided.
So this is the apex class that we have created and we have exposed it as a soap service.
Now, how are you going to call this class from our postman so how we're going to do it? Let's take a look at it.
So the first thing is, since it's been a while I'm going to authenticate myself and I will retrieve the session id now.
The next thing is I'm going to make another request here and I will be past making a post request and the url is going to be.
This is going to be the url services so and then here I'm going to save this and I'm going to say classes now so class and what is the class I wanted to call.
So if you go back to your apex, this is the name.
This is the service that I'm going to call.
So I'm saying student soap service and then I'm going to pass in the header, so the first header would be the content type.
This is what I'm going to pass and it's going to be text, slash xml, because it's a soap service and the second thing would be a soap action, and this is just going to be an empty string.
Now, what about the body it's going to be of the type xml and then I have just to save time.
I've copied it over here and then paste it, and one thing you have to do is you have to replace the session id with the one that you have just got from here, I'm going to copy the session id, because this is the one that will be required by the sales force to authenticate okay.
So this is the session id and then inside of the body.
What I'm doing is, if you look at it, I'm calling the get student.
This is the name of the method right so inside the body.
What I'm going to do, I'm going to say, get student and then I will be passing in so this is the close this get student and what do I need to pass? I need to pass the id of the student, so what is going to be the id? This is the id.
So this is the same exact name I need to use here, so I will say, student id and which student I wanted to fetch the information.
So let's say I want to fetch the information of this student and I'm going to pass in the id of the student and then I'm going to close this because my web service requires couple of things.
My web service is requiring me that I use this is the method, because how you're going to discriminate, if inside of the student soap service, if there is another method that you want to call right.
So how are you going to do that? So, in order for me to differentiate, they are going to say we are going to use that method, name that we are interested in and what that method name requires.
So if you remember, this method requires one parameter of the type student id, and this is exactly the name you have to use.
You can't use anything else, so this is the parameter name and that's what we are providing, and this is the id of the student and then I'm going to go ahead and hit the send here.
What's going to be the response, let's take a look at it, so it went fine.
The status came as 200, which is good, and now it is returning you all the information about this student.
So you can see here.
If you go back and you can verify it with the with the sales force, you can see rahul khanna.
This is his phone number.
This is his email address and this is the address.
So all that information is returned from that query that we wrote inside of the apex class now.
Another point you have to see here is: if I go ahead and change this apex class and then, if I'm changing the parameter value, the name of the parameter from student id to student std id, then what will happen is if you try to run the same thing, I have to change it in the query as well, because it didn't recognize so now you're able to save it.
Okay, it went fine now, if you're trying to call this from the postman you're going to see it's going to throw you an error and it is saying no such parameter student id defined for the operation.
So what you have to do here is you have to actually change it std id and then it will work now, because you have changed the name of the parameter, so you have to change it over here as well.
So now, if you go ahead and do the send, this will work fine, it worked like a charm.
The next thing is, if you simply change the operation.
Also, let's say instead of calling it as get student, you can say it as get student by id and if you hit the quick save here so the class got saved, everything is saved now.
The next thing is, if you try to call this again, it's going to give you a message saying no operation available for this request, because it tried to look for get student and it did not find that get student method inside of your apex class, so it has to exactly match so now I have changed it to get student by id, so I have to use the same exact operation here as well, so I have to call it as get student by id and close it with the same tags and then hit the send button.
Now the request went fine and it returned you back the response.
So this is how you create a custom soap service and you expose your class as a soap service, and this is the way you call it from postman.
Now we're going to go ahead and save this.
So we'll call this as custom student soap service and we will save it inside of this collection.
Okay, this is one just to keep the record, so this is how you create a soap service.
So, whenever you need to create a SOAP webservice in Salesforce, all you need to do is to define a static method in a global class with the keyword webservice. You can take the input as a parameter in method and the data returned by the method will be given in the response.How to create simple SOAP web service? ›
- 4.1. Maven Dependencies. ...
- 4.2. The XSD File. ...
- 4.3. Generate the Domain Java Classes. ...
- 4.4. Add the SOAP Web Service Endpoint. ...
- 4.5. The SOAP Web Service Configuration Beans.
- Steps To Integrate SOAP API In Salesforce.
- Step 1: Create a web service method on the server-side. ...
- Step 2: Generate and share the WSDL with the client.
- Step 3: Client generates apex class from the WSDL.
- Open SOAP UI, go to the File menu and click on New SOAP Project.
- You'll see a dialog as shown below:- ...
- Login to Salesforce:- Before testing any operation, our first step as to login to salesforce as we did in case of REST APIs.
- Log in to Salesforce.com as the administrator.
- Navigate to API screen.
- Click the Generate Partner WSDL link.
- The WSDL XML file is displayed. Save the XML file with the extension “wsdl” to a local directory. For example, Partner. wsdl.
When you upload and run a pipeline with a script node that uses a WebReference2 object to invoke a WSDL file, Salesforce B2C Commerce automatically generates classes from the WSDL using Apache CXF. The generated classes are automatically added to the webreferences2 package.How do you create a webservice? ›
- Go to Visual Studio then click on "File" -> "Website" -> "ASP.NET empty website template". ...
- Step 2 Add a Web Service File. ...
- To see whether the service is running correctly go to the Solution Explorer then open "Airthmatic. ...
- Step 4 Creating the client application.
To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.What is SOAP webservice format? ›
SOAP works with XML only
Web-transmitted data is usually structured in some way. The two most popular data formats are XML and JSON. XML (or Extensible Markup Language) is a text format that establishes a set of rules to structure messages as both human- and machine-readable records.
Salesforce, for example, offers both a SOAP and a REST API, allowing developers to link Salesforce with their platform in the most convenient way for them. Differences Between SOAP And REST are listed below. REST stands for Representational State Transfer.
- Generate Partner API. So from Setup, enter API in the Quick Find box, then select API. ...
- Download and Setup SoapUI. You can download SoapUI from here. ...
- Create Project in SoapUI. ...
- Login Request to Get Access Token. ...
- Generate Custom WSDL in Salesforce. ...
- Test Class for Custom WSDL.
SOAP API is used to integrate Force.com applications or other third party applications, and it lets the user to maintain passwords, perform searches and much more. You can use the SOAP API with any programming language that supports Web services.What is SOAP API with example Salesforce? ›
The Force.com SOAP API (formerly known as the Force.com Web Services API) lets you integrate Force.com applications that can create, retrieve, update, or delete records managed by Salesforce, Force.com, and Database.com, records such as accounts, leads, and custom objects.How do I expose SOAP webservice in Salesforce? ›
Expose a Class as a SOAP Service
Making your Apex class available as a SOAP web service is as easy as with REST. Define your class as global . Add the webservice keyword and the static definition modifier to each method you want to expose. The webservice keyword provides global access to the method it is added to.
- In the Logic tab, open the Integrations folder.
- Right-click the SOAP element and select Consume SOAP Web Service....
- In the displayed dialog, specify the location of the Web Service definition (WSDL) and click Consume.
- Click on the main toolbar or right-click the root node in the Navigator panel and select Import Project:
- In the Select ReadyAPIject File dialog, select the Sample-SOAP-Project-soapui-project. ...
- The sample project will be shown in the SoapUI Navigator.
The major difference between SOAP and REST API include:
REST API uses Web Application Description Language to describe service. REST is more flexible but it also requires standardization. SOAP-based reads cannot be cached. REST has better performance and scalability.