You must have Javascript enabled to experience this site.

Setting Salesforce SessionID with AXIS

Posted & filed under Coding Tips.

Connecting to a Salesforce Web Service is not as obvious as it should be.

While logging on to Salesforce is well described (see, it is not so obvious what to do next. The two non-obvious things that seem to throw most people are:

1) You have to change the end point URL to that returned by the login process.
2) You must set the Salesforce SessionID in the SOAP header.

The first point is covered in the documentation, it is just not as clear as it should be. The second point is what really confuses people.

Firstly the name SessionID immediately makes people think of a HTTP SessionID which has nothing to do with this. Salesforce should have used another name (say ConnectionID) to avoid this confusion. Then it is not obvious what you need to do with it.

The following code is only a fragment and will not work ‘as is’ – you will have to adapt it for your environment. After you have logged in, change the endpoint and set the SessionID in the SOAP header. You can then use your web service (which in this example is MyService with a function of myWebServiceFunction).

import com.sforce.soap.partner.SessionHeader;
import com.sforce.soap.schemas._class.WebService_MyServiceWebService_MyServiceBindingStub;
import com.sforce.soap.schemas._class.WebService_MyServiceWebService_MyServiceServiceLocator;

// my logon to Salesforce class
SalesforceLogon Sforce = new SalesforceLogon();
String endpoint = Sforce.getWebserviceURL();
String sessionid = Sforce.getSessionId();

// define web service endpoint
WebService_MyServiceServiceLocator wsloc = new WebService_MyServiceServiceLocator();
WebService_MyServiceBindingStub ws = (WebService_MyServiceBindingStub) wsloc .getWebService_MyService();

// define SOAP header for Salesforce
SessionHeader sh = new SessionHeader();
ws.setHeader(wsloc.getServiceName().getNamespaceURI(), "SessionHeader", sh);

// call web service function
boolean rc = ws.myWebServiceFunction();