[SalesForce] how to get the session Id for web service calls to salesforce

I have implemented a web service in salesforce and generated the WSDL for the web service and provided it to the external system. However while invoking a call, we need to pass the session id in teh request. How can the external system get the session Id? Can we create a connected app and provide them the required details to get the session id?

Best Answer

First you need to set your endpoint of your server.

  1. Endpoint URL (Sandbox)-> https://test.salesforce.com/services/Soap/u/35.0
  2. Endpoint URL (Production)-> https://login.salesforce.com/services/Soap/u/35.0

Set your headers as below:

  1. SOAPAction-> ""
  2. Content-Type-> text/xml

And finally Body. Replace Login Id and Password with actual credentials:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
   <soapenv:Body>
      <urn:login>
         <urn:username>Login ID</urn:username>
         <urn:password>Password</urn:password>
      </urn:login>
   </soapenv:Body>
</soapenv:Envelope>

In response, you will get SessionId.

Once you've SessionId, set your endpoint URL as follows:

https://domain_name.my.salesforce.com/services/apexrest/YourWebServiceName

Again set Headers as follows:

  1. Authorization-> Bearer
  2. Content-Type-> application/json

And Body:

{
  "name":"ABC"
}
Related Topic