+ Start a Discussion
bdr_09bdr_09 

Java Client Code for Apex Web service Methods...

Hello All,

               I have two apex web service methods and am unable to call these methods in java.
can any body suggest me how can use with java client program?

Thanks,
--bdr_09
Best Answer chosen by Admin (Salesforce Developers) 
Chirag MehtaChirag Mehta

Hey guys,

 

I got the solution. It goes as follows ...

 

Step1 : Login to SFDC, using Enterprise/Partner WSDL and obtain SessionId

binding1 = (SoapBindingStub) new SforceServiceLocator().getSoap();
loginResult = binding1.login("username","PwdToken");
SessionHeader sh = new SessionHeader();
sh.setSessionId(loginResult.getSessionId());

 

Step2 : Set above fetched SessionId as the sessionHeader on your Apex Webservice WSDL stub and then call the required methods.

binding2 = (checkForLeadNumberBindingStub) new checkForLeadNumberServiceLocator().getcheckForLeadNumber();
binding2.setHeader(new checkForLeadNumberServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
binding2.extractLeadId("123456","123456");

 

So that's it, you are all done! nJoy!!

 

Note : binding1 and binding2 are pointers to Enterprise WSDL and Apex Webservice WSDL respectively.

private SoapBindingStub binding;
private checkForLeadNumberBindingStub binding2;

Message Edited by Chirag Mehta on 05-24-2009 09:45 PM

All Answers

SuperfellSuperfell
use the regular partner or enterprise APIs to call login, take the sessionId from the LoginResult, set it as the sessionHeader on your apex WS stub, call the methods.
Kumar_14279Kumar_14279

hi Simon, I tried the same approach for calling the custom apex webservice from java. I created SessoinHeader from Enterprise.wsdl SforceServiceStub and tried to set it to SessionHeader object of my webservice. Please help me on this.

 

Thank You,

K. Mani Kumar.  

Chirag MehtaChirag Mehta

 

Code for LOGIN call, 

 

binding = (SoapBindingStub) new SforceServiceLocator().getSoap();

loginResult = binding.login("USERNAME","PWD");

SessionHeader sh = new SessionHeader();
sh.setSessionId(loginResult.getSessionId());
binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
 

Now Simon can you please help on how to use above generated session Id tand how to set it as the sessionHeader on your apex WS stub, call the methods.?

Chirag MehtaChirag Mehta

Hey guys,

 

I got the solution. It goes as follows ...

 

Step1 : Login to SFDC, using Enterprise/Partner WSDL and obtain SessionId

binding1 = (SoapBindingStub) new SforceServiceLocator().getSoap();
loginResult = binding1.login("username","PwdToken");
SessionHeader sh = new SessionHeader();
sh.setSessionId(loginResult.getSessionId());

 

Step2 : Set above fetched SessionId as the sessionHeader on your Apex Webservice WSDL stub and then call the required methods.

binding2 = (checkForLeadNumberBindingStub) new checkForLeadNumberServiceLocator().getcheckForLeadNumber();
binding2.setHeader(new checkForLeadNumberServiceLocator().getServiceName().getNamespaceURI(), "SessionHeader", sh);
binding2.extractLeadId("123456","123456");

 

So that's it, you are all done! nJoy!!

 

Note : binding1 and binding2 are pointers to Enterprise WSDL and Apex Webservice WSDL respectively.

private SoapBindingStub binding;
private checkForLeadNumberBindingStub binding2;

Message Edited by Chirag Mehta on 05-24-2009 09:45 PM
This was selected as the best answer
bubkirchnerbubkirchner

This is very useful info. 

 

Question: Once you have established the two bindings, can you use binding1 to do "normal" non-Web Service calls, like binding1.create(Account) and binding2 to do Apex Web Service calls?

 

On a related topic, I'm getting the following errors when running two Java apps in the same web server using the same user and I'm caching the session info so I can re-use.  Is this OK or should I use two separate users for each app?  Thanks for any advice.

 

2011-12-20 14:46:00,833 ERROR [com.cort.sf.model.SfBase] - Failed to create Account succesfully, error message was: 

Invalid Session ID found in SessionHeader: Illegal Session. Session not found, missing session key: 00D70000000J3Ew!AQYAQPsLcKyTqKTUnNftoABV1FZ857HiJWTRVVw_7Jtm1VfaQinx0TVBY771ut2yGgvJY6kMrTW.XEeQwMYizQ5JtKdzZBlU