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
Alex.AcostaAlex.Acosta 

API Session Id for external integration

API Session Id

 

I've been trying to find a work around for this issue. Basically, if I use the merge field {!$Api.Session_ID} on a button, the session Id is value and works just fine. If I attempt to use the same Api.Session_ID within either a formula field on a sObject, or within the APEX VisualForce page as a standalone merge field the session is no longer valid and fails when an outside source attempts to use that session Id. Would anyone know why and/or a work around for this?

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

The problem here is that a Salesforce Session Id is associated with a particular end-point server. In the case of the button on the Visualforce page, the session id you'd get from an Apex UserInfo.getSessionId() call would be associated with the Visualforce server you're interacting with. That's a problem because the Report servlets from which we retrieve report data aren't available from the Visualforce servers -- only from the "front-end" servers (na1, na2, emea, etc.) .

All Answers

_Prasu__Prasu_

Are you passing  Session Id from URLusing that field? are you performing url decode and encode on anyside?

Alex.AcostaAlex.Acosta

There are 2 ways I attempted this

 

1) Within the VF page I was trying to generate my URL:

 

<apex:commandButton value="PDF" onclick=" window.open(http://https://www.appextremes.com/apps/Conga/PointMerge.aspx?sessionId={!Api.Session_Id}{urlString})" />

 

 

2) Within Apex Code:

 

Since I was unable to find a way to get the API session Id within my the Apex Class side, I just make a formula field which had the value you see above to dynamicly generate my session Id. The reason for this is because if i'm not mistaken the user session id is different from the api session id. So within Apex my logic looked something as such 

 

String urlString = 'https://www.appextremes.com/apps/Conga/PointMerge.aspx'+'?sessionId='+EncodingUtil.urlEncode( crd.API_Session_Id__c, 'UTF-8')+'&serverUrl='+EncodingUtil.urlEncode(crd.Partner_Server__c, 'UTF-8')+'&id='+crd.Id +'&ReportId=00OP0000000Wrwy?pv0='+crd.Name;

 

Both of these failed. But if i used the same logic on a button on the sObject that you can put on a page layout ie:

https://www.appextremes.com/apps/Conga/PointMerge.aspx
?sessionId={!API.Session_ID}
&serverUrl={!API.Partner_Server_URL_80}
&id={!CRD__c.Id}
&ReportId=00OR0000000grsJ?pv0={!CRD__c.Name}

 and copied my session Id from what this button generated the used it for both examples above, it would work.

 

 

Ispita_NavatarIspita_Navatar

Hi,

           When i tried the following code snippet at my end on VF page by giving the session id it worked.Please follow this sample code it wil help resolve your problem:-.

           <script src="/soap/ajax/23.0/connection.js" type="text/javascript"></script>

           <script type="text/javascript" src="/js/functions.js"></script>

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

                                   alert(' Session Id ' + '{!$Api.Session_ID}');

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

Alex.AcostaAlex.Acosta

Thanks for the reply and I attempted to implement this, but sadly it did not work. I'm trying to provide the session id as a parameter in the url to conga composer.

Alex.AcostaAlex.Acosta

The problem here is that a Salesforce Session Id is associated with a particular end-point server. In the case of the button on the Visualforce page, the session id you'd get from an Apex UserInfo.getSessionId() call would be associated with the Visualforce server you're interacting with. That's a problem because the Report servlets from which we retrieve report data aren't available from the Visualforce servers -- only from the "front-end" servers (na1, na2, emea, etc.) .

This was selected as the best answer
Dev@!#Dev@!#

Hi Alex,

 

I am facing the same problem

 

Were you able to figure out how to pass the session id and URL?

Alex.AcostaAlex.Acosta

If this is for conga, you'll need to install their conga query app and use that rather than reports.

 

Direct Link to install: https://login.salesforce.com/?startURL=/packaging/installPackage.apexp?p0=04t70000000cbJu

Or go to their site http://home.appextremes.com/support/ > click Installations sub tab, and scroll all the way to the bottom to find the link

Gooch ForeverGooch Forever

HI, I want to know what to write in onclick javascript  custom button; so that after clicking I would get whether the session is expired or not ??????

 PLease post ASAP

Tomas.RayTomas.Ray

try this

 

{!requireScript("/soap/ajax/26.0/connection.js")}
alert(sforce.connection.sessionId);