I been trying to get my Session ID Parameters into my Apex class so that I can retrieve a Picture from inside Salesforce and send it out to Facebook.

But I'm not sure why I'm not getting my Session ID Can someone help me?

I been using this 2 posts as reference.

VisualForce Page Code

<apex:page controller="GetChart" showHeader="true" sideBar="true" >
<apex:includeScript value="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"/>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" />

<script type="text/javascript">

function chk123() { 
<apex:sectionHeader title="Attach Documents"/>

<apex:form enctype="multipart/form-data" id="theform">
<apex:pageMessages />
<apex:pageBlock title="Upload a Attachment">

<apex:inputHidden value="{!myString}" id="myHiddenField"/>

    <apex:actionFunction name="passSessionParameters" action="{!exInt}" rerender="myHiddenField"/>
  <apex:pageBlockButtons >
    <apex:commandButton value="Save" onclick="chk123();"/>


  <apex:includeScript value="/soap/ajax/29.0/connection.js"/>
  <apex:includeScript value="/soap/ajax/29.0/apex.js"/>
  <script type="text/javascript"> 
var __sfdcSessionId = '{!GETSESSIONID()}';
var oResult = sforce.connection.query(query); 
var oDocuments = oResult.getArray('records'); 

<apex:outputPanel rendered="{!bolClose}">
         opener.location.href = "/apex/Upload_Attachment?id="+"{!aId}"; 

Apex Class Page

public with sharing class GetChart{

public String aId { get; set; }

public Boolean bolClose { get; set; }

public String objAtt { get; set; }

public String resume { get; set; }

public String intSize { get; set; }

public String strFileName { get; set; }

public String myString {get; set;}

public GetChart(){
    myString = '';

public PageReference exInt() {

bolClose = false;

String chart_1 ='https://ap4.salesforce.com/servlet/servlet.ChartServer?r....';

String base64Data;
httprequest req = new httprequest();
req.setHeader('Cookie', 'sid=' + myString + ';'); //UserInfo.getSessionId()+ ';');
system.debug('@@sid ' + myString);
system.Debug('@@Session = ' + UserInfo.getSessionId());
http http = new http();
httpresponse res = http.send(req);
Blob image = res.getBodyAsBlob();
base64Data =  EncodingUtil.base64Encode(image);

Document docatt = new Document();
docatt.Name = 'Chart 1.jpg';
docatt.Body = EncodingUtil.base64Decode(base64Data);
docatt.FOLDERID = '42l6F03184wqNi'; //Your folder id
docatt.IsPublic = true;

insert docatt;

return null;


  • are trying to get session id from javascript ?
    – Avijit Das
    Apr 27, 2017 at 9:20
  • Yes I'm trying to get the session from the javascript unless there is another way to get it from the class? Because I tried UserInfo.getSessionId() but it didn't work
    – Luke Au
    Apr 27, 2017 at 9:21

2 Answers 2


if you want the session id from javascript then use below code snippent: -

<apex:page >
 <script type="text/javascript">
        var sfdcSessionId = '{!GETSESSIONID()}';

it is working in my DE.

  • I modify the code with yours but from what I see from the debug logs the value isn't passing over to the Apex Class. I say this because when I CTRL F in the debug log i cannot find @@sid or @@Session =
    – Luke Au
    Apr 27, 2017 at 9:28
  • @LukeAu - Why can't you use userInfo.getSessionId()
    – Eric
    Apr 27, 2017 at 11:42
  • I tried that but when i see the image in the document folder it give me a blank image & not the image in the url
    – Luke Au
    Apr 27, 2017 at 11:43

There are restrictions on the usage of the UserInfo.getSessionID() method - if you are calling it during asynchronous apex execution - for example a scheduled job or an @future method, it only returns a blank value. If you haven't considered this already, you might want to, while debugging. Thanks!

  • can you also add a reference for your findings?
    – itsmebasti
    Nov 17, 2017 at 11:03
  • You can find this documented by Salesforce here. There are some StackExchange threads on this too, you can find one of them here. As for a workaround (that worked for me, too), check out Phil Hawthorn's answer here. Nov 18, 2017 at 11:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .