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 http://wiki.developerforce.com/page/Screencast:Authenticating_against_the_Force.com_Web_Services_API_using_Java_and_Apache_Axis), 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();
wsloc.setWebService_MyServiceEndpointAddress(endpoint);
WebService_MyServiceBindingStub ws = (WebService_MyServiceBindingStub) wsloc .getWebService_MyService();

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

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

 

 

 

 

 

24 views