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?
[SalesForce] how to get the session Id for web service calls to salesforce
Related Solutions
You don't need to pass the Request/Response into the method. I would have written this like:
@RestResource(urlMapping='/GetAccountDetails/*')
global with sharing class GetAccountDetails
{
@HttpGet
global static List<Account> getManagers()
{
RestRequest req = RestContext.request;
String memberID = req.requestURI.substring(req.requestURI.lastIndexOf('/') + 1);
return [SELECT Id, Name, Member_c, OwnerID FROM Account WHERE Member__c = :memberID];
}
}
This assumes you are passing an additional String into the parameters.
And would also like to know how do I invoke this service at the external system, like, by any URL call or any other way.
You could use a SOAP service if you want, but IMO REST is usually the better way to go.
EDIT: @sfdcfox also pointed out that returning the native object allows the caller to specify a JSON or XML response.
EDIT 2: String version of the query.
String query = 'SELECT Id, Name, Member_c, OwnerID FROM Account WHERE Member__c = :memberID';
return Database.query(query);
/* you could also cast it to Account before returning
List<Account> result = Database.query(query);
return result;
*/
You will need to set the endpoint for the client based on the LoginResult serverUrl
.
The login call will go to login.salesforce.com. When it comes back the serverUrl will be something like https://na5.salesforce.com/services/Soap/u/34.0.
partnerSoapSforceCom.Soap partner = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult lr = partner.login('user@example.com', 'SomePassword' + 'SomeSecurityToken');
ARTaskWebService.AR_getActivitiesWebservice client = new ARTaskWebService.AR_getActivitiesWebservice();
client.SessionHeader = new ARTaskWebService.SessionHeader_element();
client.SessionHeader.sessionId = lr.sessionId;
// You may need to adjust this if using a custom Apex Soap service.
client.endpoint__x = lr.serverUrl;
Since your Apex class Soap class probably has an endpoint like https://na8.salesforce.com/services/Soap/class/AR_getActivitiesWebservice
you may need to adjust this using the domain in the LoginResult serverUrl.
E.g. replace the na8 pod identifier in your current endpoint with na5 if that is what the serverUrl comes back with.
Best Answer
First you need to set your
endpoint
of your server.https://test.salesforce.com/services/Soap/u/35.0
https://login.salesforce.com/services/Soap/u/35.0
Set your
headers
as below:And finally
Body
. Replace Login Id and Password with actual credentials: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:And
Body
: