0

I'm getting a different Session ID value when I use my VisualForce Page to run my Apex Class. Before that I been testing my codes using the Developer's Console and I could get my Chart from Salesforce Dashboard save into the Document folder but when tried using the VisualForce Page to run it the image does not get save into the Document folder.

I'm doing this because I want to convert my code to be able to work in the VisualForce Page so that other employees in the company can use this.

Can someone Help me Fix it?

When I use the VisualForce Page I get this Session ID from the debug logs

09:58:12.2 (53964823)|USER_DEBUG|[37]|DEBUG|@@Session = 00D6F000001vFKh!ARAAQC4YPK9T.Hky.5ZhpDiC5vOAZR5rkg66nEvEwJjjihNm.dQlrm51eQGenrQWrySWvT9eYvxu0_i4szsN0uN.eDji7w0D
09:58:12.2 (180371499)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:489
09:58:12.2 (180477308)|CALLOUT_RESPONSE|[39]|System.HttpResponse[Status=Server Error, StatusCode=500]
09:58:12.2 (180489678)|HEAP_ALLOCATE|[39]|Bytes:16
09:58:12.2 (180511277)|VARIABLE_SCOPE_BEGIN|[39]|res|System.HttpResponse|true|false
09:58:12.2 (180598918)|VARIABLE_ASSIGNMENT|[39]|res|"System.HttpResponse[Status=Server Error, StatusCode=500]"|0x3653bc
09:58:12.2 (180604236)|STATEMENT_EXECUTE|[40]
09:58:12.2 (180690551)|VARIABLE_SCOPE_BEGIN|[40]|image|Blob|false|false
09:58:12.2 (180705041)|VARIABLE_ASSIGNMENT|[40]|image|BLOB(0 bytes)

I get this Image when using VisualForce enter image description here

When I use the Developer's Console I get this Session ID from the Debug logs

09:59:50.3 (16293460)|USER_DEBUG|[37]|DEBUG|@@Session = 00D6F000001vFKh!ARAAQEb1ZWJVDeLgxTsZTUX2Ktr82l6KLvBfAvvZ.B0AYo0_ljYrwLJI4WMh3be.KXhQbYXGG3w2bXhPPjnx8IDfs1wNmGDB
09:59:50.3 (220011150)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:15598
09:59:50.3 (220165375)|CALLOUT_RESPONSE|[39]|System.HttpResponse[Status=OK, StatusCode=200]
09:59:50.3 (220180940)|HEAP_ALLOCATE|[39]|Bytes:14976
09:59:50.3 (220206477)|VARIABLE_SCOPE_BEGIN|[39]|res|System.HttpResponse|true|false
09:59:50.3 (220318010)|VARIABLE_ASSIGNMENT|[39]|res|"System.HttpResponse[Status=OK, StatusCode=200]"|0x4685df66
09:59:50.3 (220326581)|STATEMENT_EXECUTE|[40]
09:59:50.3 (220389471)|HEAP_ALLOCATE|[40]|Bytes:4
09:59:50.3 (220420962)|VARIABLE_SCOPE_BEGIN|[40]|image|Blob|false|false
09:59:50.3 (220440064)|VARIABLE_ASSIGNMENT|[40]|image|BLOB(14970 bytes)

I get this image when using the Developer's Console enter image description here

Codes I use in the Developer's Console

GetChart g = new GetChart();
g.exInt();

Apex Class Codes

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.setEndpoint(chart_2);
req.setmethod('GET');
req.setHeader('Cookie', 'sid=' + 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;

}

}

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() { 
jQuery('[id$=myHiddenField]').val('{!GETSESSIONID()}');
passSessionParameters();
}
</script>
<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" rerender="myHiddenField"/>
  <apex:pageBlockButtons >
   <apex:commandButton value="Save" action="{!exInt}" onclick="chk123(); return false;"/>


  </apex:pageBlockButtons>

  <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'); 
</script>

<apex:outputPanel rendered="{!bolClose}">
      <script>
         alert("{!aId}");
         self.close();
         opener.location.href = "/apex/Upload_Attachment?id="+"{!aId}"; 
      </script>
      </apex:outputPanel>
     </apex:pageBlock>
 </apex:form>
 </apex:page>

I just found this post and the question/comment that was post by @ddkpe seems to be similar to what I'm facing.

  • How do you access your VF page? Did you expose this by any chance to the guest site user and access the page as an unauthenticated user? – Jannis Bott Apr 28 '17 at 2:12
  • I acess it by using the preview button after creating the visualforce page.Currently I'm the only 1 accessing this page to do all the testing and stuff. – Luke Au Apr 28 '17 at 2:15
  • It's common to have multiple session ID values while you're logged in to salesforce (e.g. one for salesforce, one for the API, one for Visualforce). Your session ID is likely not your issue. – sfdcfox Apr 28 '17 at 3:42
  • I tried hard coding the Session ID from the Developer's Console like this and it work. req.setHeader('Cookie', 'sid=' + '00D6F000001vFKh!ARAAQEb1ZWJVDeLgxTsZTUX2Ktr82l6KLvBfAvvZ.B0‌​AYo0_ljYrwLJI4WMh3be‌​.KXhQbYXGG3w2bXhPPjn‌​x8IDfs1wNmGDB' + ';'); unlike the Session ID from the VisualForce page. I hard coded the Session ID and run it from the VisualForce Page. But If this Session ID work why won't the other Session ID work? & is there any other way around it? – Luke Au Apr 28 '17 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.