[SalesForce] SOAP API- Inbound call to salesforce

Am new to integration ,and can code in APex. I have written a apex class exposed as webservice to call. As i understand : to get an inbound call to salesforce – this apex codes wsdl needs to shared with external application.

ALso, while doing this found WSDL was quite absurd -had lot of info that required. So I have to modify my wsdl as there were lot of unwanted entities which was not part of my apex code. is there any alternative way of getting rid of unwanted entities?

Post manually modifying wsdl I tested it using SOAP UI,but it keeps getting the invalid session ID error.

Am not sure– how to resolve this error? I dont have answers to my below queries:

  • In SOAP UI– how can we pass a session ID?
  • How can I test this by removing session ID and using client certificate?

Error: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session<

Best Answer

You will need to get an active Salesforce session id and know which instance it comes from. You can do this in a number of ways, such as:

  • using one of the oauth flows (generally the preferred method),
  • by calling login on the partner/enterprise API,
  • via a canvas app (signed request),
  • or just copying the id from the current sid cookie.

Once you have it, you will need to send that value in the SessionHeader.

For example, take the web service:

global class TimeWebService {
    webService static string getTime() {
        return DateTime.now().formatLong();
    }
}

Just for demonstration purposes, I generated the Apex classes to call it. Invoking it with anonymous Apex:

soapSforceComSchemasClassDfbTimeweb.TimeWebService ts = new soapSforceComSchemasClassDfbTimeweb.TimeWebService();
ts.SessionHeader = new soapSforceComSchemasClassDfbTimeweb.SessionHeader_element();
ts.SessionHeader.sessionId = UserInfo.getSessionId();
String serverTime = ts.getTime();
System.debug(serverTime);

Note here that the Session Id was taken directly from the active Salesforce Session and that the web services endpoint default to that from the WSDL. In my case: https://na5.salesforce.com/services/Soap/class/DFB/TimeWebService. In an external application you will need to determine the SessionId and instance(na5) components via other means.

The XML Soap Request was:

<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <env:Header>
    <SessionHeader xmlns="http://soap.sforce.com/schemas/class/DFB/TimeWebService">
      <sessionId>00D700000000001!AQoAQHvUJOLpAbU1d_NOTAREALSESSIONID_KlHXaMdj</sessionId>
    </SessionHeader>
  </env:Header>
  <env:Body>
    <getTime xmlns="http://soap.sforce.com/schemas/class/DFB/TimeWebService" />
  </env:Body>
</env:Envelope>

Note the env:Header containing the SessionHeader and sessionId.