+ Start a Discussion
rtscottrtscott 

Get Current SessionID with AJAX Toolkit

Hello All,

 

I would like to create an HTML file that a Salesforce user can click on while logged into the system that will grab the user's Session ID and append it to the document. I believe I can do this with the AJAX Toolkit, but I am not sure how to make it happen. For example: let's say that I attach the following HTML file to a Case in Salesforce.com:

<!doctype html>
<html>
<head>
<script src="/soap/ajax/16.0/connection.js" type="text/javascript"></script>
<script type="text/javascript">
function initPage() {
var sid = sforce.connection.sessionId;
document.getElementById('output').innerHTML = +sid;
}
</script>
</head>
<body onload="initPage()">
<h1>Session id is: <span id="output"></span></h1>
</body>
</html>

If a Salesforce user opens this file, I want their Session ID to show up on the page. I have tried to do this, but I keep getting back "0" in my output (i.e. the page says "Session id is: 0").

 

Am I doing this wrong? Is there any way to make this happen?

 

Thanks for any info you can provide.

 

-- Robert 

 

Message Edited by rtscott on 09-01-2009 02:16 PM
Message Edited by rtscott on 09-01-2009 02:18 PM
Message Edited by rtscott on 09-01-2009 02:20 PM
CaptainObviousCaptainObvious

Looks good, however- try:

 

 

document.getElementById('output').innerHTML += sid;

 

 :D

 

rtscottrtscott

Thanks - looks like I'm getting closer with that change. However, now the page outputs:

 

Session id is: null

 

To do this, I attached that HTML file to a Case record in Salesforce, and while logged into Salesforce I viewed the attachment.

 

Thanks for your response!

 

-- Robert

CaptainObviousCaptainObvious

How did you "attach" the file?

 

I tried your code in an s-control and used a custom link to get access to it. The session id populates correctly.

rtscottrtscott

I went to an existing Case record, and on the "Google Docs & Attachments" related list I clicked on the "Attach File" button. I selected my HTML file with the code listed above and attached it to the Case. Once the file was attached, I was able to "view" the attached file from the Case record. The file is hosted on SFDC servers and opens in the same browser that I am logged into the system with (although it opens in a different tab/window).

 

See the attached screenshots for more info.

 

Will this not work?

 

Thanks,

 

-- Robert  

 

 

Message Edited by rtscott on 09-02-2009 10:26 AM