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
prasad_more1.3158103303596086Eprasad_more1.3158103303596086E 

How to get logged in User session using AJAX toolkit

Hi,

 

I'm using AJAS toolkit to call Apex class method from Javascript. The javascript used for calling Apex class method is added in SFDC using Custom Home Page component.

 

To call Apex class method, it is mandatory to use "sforce.connection.login" method to get sessionId. The login method accepts username, password and token to authenticate and create session. Once authentication is successful, I can use "sforce.apex.execute" method to call Apex class method.

 

I wish to skip call to "sforce.connection.login" and use logged-in users session so that I don't have to specify User Name, password and token in javascript. Is there any way I can use logged-in Users session in AJAX toolkit. Any help in this regard is appriciated.

 

Thanks,

Prasad

 

 

Rahul SharmaRahul Sharma

hi Prasad_more,

 

try using this:

sforce.connection.sessionId = '{!$Api.Session_ID}';

 

Venkat@12Venkat@12

Hi,

 

I Too tried to find if the logged in user is valid using the below code

 

try

{

var

result = sforce.connection.login("myname@myemail.com", "password");

log(

"logged in with session id " + result.sessionId);

}

catch(error) {

if

(error.faultcode.indexOf("INVALID_LOGIN") != -1) {

log(

"check your username and passwd, invalid login");

}

else {

log(error);

}

}

 

I tried giving my correct userName and password.. but it still went to the catch block.

 

Please help.

 

Thanks,

Venkat