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
onlinenandaonlinenanda 

How to get Session ID and Server url from Visual force page to Controller class when page loads

I need to set Session ID and Server URL when page loads in controller class. This is required to call external synchronous web service call
aalbertaalbert

Check out this blog post.

 

onlinenandaonlinenanda

right now i am using this blog post only, here i need to click the output text field to get server url and session id, what i need is something like on page load without clicking anything on page. I mean when page loads first time i need to get server url and session id.

 

is there any way or woraround for my requirement..

Eric_SantiagoEric_Santiago

*Bump*

 

Working on this same issue right now. To further complicate this I 'm have a component that need to get this info and I'd really not require user interaction. Any suggestions?

onlinenandaonlinenanda
Try Following







<script> var btnCopy = document.getElementById("{!$Component.lk}"); </script>

<script>
window.onload = new function() { btnCopy.click(); };
</script>
onlinenandaonlinenanda
Try Following
Eric_SantiagoEric_Santiago

Thanks for the suggestion but I took a different route using the onload event. The following works in a VF page;

 

 

<apex:page setup="true" controller="MyController" showHeader="true"> <apex:form> <apex:actionFunction name="getAPIParams" id="getAPIParams" action="{!dologin}" reRender="theForm" > <apex:param name="sessionId" assignTo="{!apiSessionId}" value="{!$Api.Session_ID}" /> <apex:param name="serverURL" assignTo="{!apiServerURL}" value="{!$Api.Partner_Server_URL_140}" /> </apex:actionFunction> <script language="JavaScript"> window.onload = function() { getAPIParams();} </script> </apex:form> </apex:page>

 

 

public class MyController { public String apiSessionId {get;set;} public String apiServerURL {get;set;} public PageReference doLogin(){ System.debug('apiSessionId: ' + apiSessionId); System.debug('apiServerURL: ' + apiServerURL); return null; } }

 

Check the System Log to verify the outputs. It also seems to work when nested in a component using the same controller.

 

 

<apex:page setup="true" showHeader="true"> <c:testServerParams /> </apex:page>

 

<apex:component controller="MyController"> <apex:form > <apex:actionFunction name="getAPIParams" id="getAPIParams" action="{!dologin}" reRender="theForm" > <apex:param name="sessionId" assignTo="{!apiSessionId}" value="{!$Api.Session_ID}" /> <apex:param name="serverURL" assignTo="{!apiServerURL}" value="{!$Api.Partner_Server_URL_140}" /> </apex:actionFunction> <script language="JavaScript"> window.onload = function() { getAPIParams();} </script> </apex:form> </apex:component>

 

 

 The key I found was the reRender attribute on the ActionFunction. Without it the variables don't get passed back to the controller.

 

 

 

ShikibuShikibu

Thanks for the above code. There's a bug in it; you need to set the id of form element to theForm.

Prakash@SFDCPrakash@SFDC

Is there any standard methods to retrive the apiSessionId and apiServerURL values directly in class.