How to Expose Apex Class as a SOAP Service in Postman? | Salesforce Developer Guide
Before starting this first we discuss why we expose an apex class as SOAP service. The reason for exposing the class as soap is an external system and using the soap service, the apex class can call the Salesforce and access its data.
There are some rules to expose class as soap.
- You have to define your class as global. (don’t use public keyword here)
- You have to decorate your method with the web service keyword and you have to make the method static.
global with sharing class soapServiceClass{ Webservice static Account getAccountRecord(string id){ } }
Webservice keyword provides global access to the method. It makes your method global so that external systems (outside of Salesforce) will be able to access this method.
Let’s take an example & in this example we are going to create an apex class and expose it as SOAP & call it from the Postman.
Don't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial
Step 1:
First we have to create an apex class in Salesforce (Go to developer console → create new apex class).
global with sharing class soapServiceClass{ //Based on this parameter, it returns the account information webService static Account getAccountRecord(string accid){ Account acc = [select id, name, type, AccountNumber, Rating, AnnualRevenue from account where id=:accid]; return acc; } }
Step 2:
We know that when we send a username & Password to the Salesforce login And in response we get the server url & session id.
Let’s create a new request to get the account information.
EndPoint Url:
Url format: server url /class / ApexClassName (we are going to get this server url from the authentication call)
Method Name : POST
Header: It includes the Content-Type and SOAPAction. (SOAPAction should be an empty string and content-Type is text/xml).
Request Body:
<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/schemas/class/soapServiceClass"> <soapenv:Header > <SessionHeader> // if your sessionId is expired then you have to replace it with the latest sessionId. // session id is required for salesforce to authenticate. <sessionId> 00D5g000005GOj5!ARoAQJ_JdUnRViliE01XeQ3wM5fzKw5.R7b13bdGVEDTBbN0W8XGMvmQeA_BXZQLsbdpVUe3bICLxwIDRvF0OvtHDlPi_8lz </sessionId> </SessionHeader> </soapenv:Header> <soapenv:Body> //Pass the Apex class method name here and also inside this method we have to pass the account id (Id of that account whose details you want to see in the postman <getAccountRecord> //You have to use the same name here that you pass in the method as a parameter. <accid> Your account Id here. (whose details you want in the postman) </accid> </getAccountRecord> </soapenv:Body> </soapenv:Envelope>
Check out another amazing blog by Shweta here: How to Connect Salesforce and Postman Using SOAP API
Note:
- Suppose you have add this <accountId> </accountId> instead of this <accid></accid> in the XML body then you will receive an error message like this “ No such parameter accountId is defined for the operation.”
- if you have passed the wrong method name then it also throws an error message like “No such Operation found”.
So make sure you have entered the correct method name and pass the exact parameter name in the XML body.
After setting up the endpoint url, header, body and request type. Now, Click on Send button then you will receive account information in the response.
Hey I am getting below error and the url I am using as below
URL:- https://namscape.sandbox.my.salesforce.com/services/Soap/u/58.0/00D56000000N6b5/class/soapServiceClass
Error :-
soapenv:Client
No operation available for request {http://soap.sforce.com/schemas/class/soapServiceClass}getAccountRecord
Hi Pratik,
I am facing the same issue :-
"No operation available for request {http://soap.sforce.com/schemas/class/soapServiceClass}getAccountsWithSoap, please check the WSDL for the service."
Were you able to find the resolution.
Thanks,
Parikshit