function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
manojjain4328manojjain4328 

How to write login() method code in Apex?

Hi,

 

I am trying to implement initial Helloworld webservice in Salesforce.

But i got error:

 

"Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session faultcode=sf:INVALID_SESSION_ID faultactor="

 

 

After that i have tried to write login() method for getting valid session id so i took reference of sample java method

 

Sample Code—Java

 

privateboolean login()

{

LoginResult loginResult = null;

SoapBindingStubbinding = null;

try {

// Createbinding object

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

 

loginResult =binding.login("username", "password");

    }

catch (Exception ex){

System.out.println("An unexpected errorhas occurred." + ex.getMessage());

return false;

}System.out.println("Login was successful.");

 

// Resetthe SOAP endpoint to the returned server URL

 

binding._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());



// Createa new session header object // add the session ID returned from thelogin

 

_SessionHeader sh = new _SessionHeader(); sh.setSessionId(loginResult.getSessionId());

// Set the sessionheader for subsequent call authentication

 

binding.setHeader(new SforceServiceLocator().getServiceName().getNamespaceURI(),

"SessionHeader",sh);

 

//get user info

 

try {

GetUserInfoResultuserInfo = binding.getUserInfo(); }

 

catch (Exception ex) {

System.out.println("Anunexpected error has occurred." + ex.getMessage()); returnfalse;

}return true;

}

 

 

But when i tried to implement this same code in Apex i got many error bcoz it is in java .

I just want to know hoe can we write login method in Apex so tat could get valid session id to implement my webservice.

 

Regards

Manoj 

 

 

 

 

Anand@SAASAnand@SAAS
Are you trying to call a Salesforce Login API from Apex? I don't know if that's going to work as easily as calling any other web service?
mtbclimbermtbclimber

The answer here lies with what you are actually trying to do.  If you want to generate a sessionId then use UserInfo.getSessionId() .

 

If you want to call out to a webservice from Apex that is synchronous so instead of sending a sessionid why not just return the information/instructions in the callout response?

 

What are you trying to accomplish? 

SuvraSuvra

Hi,

 

I have a similar kind of problem.

 

In Salesforce, I have one Session ID and Server URL. I need to write one web service, which will take the Session Id and Server URL as its input and will return the User ID (associated to the Session Id provided).

 

 

Please provide your idea on this.

 

Thanks in Advance,

Suvra