2

i am stuck in a problem.i have to call rest api but i have to get session id from a visual force page .in cookies there is sid from where i can get session id.but from where i get the instance_name of org.suppose i am trying to create a chrome extension.in which i have to call rest api and dont want to use oauth for extension instead when an authorized page visited then this extension is visible omnibox extension.please guideline how to get instance_name like ap1 etc in visualforce page from javascript ??

4

Since you have access to Visualforce, you can have the Visualforce page pass System.Url.getSalesforceBaseUrl().toExternalForm() to your JavaScript, or you could set up a @RemoteAction to obtain the same information. You can also just ask for the session ID in Visualforce as well instead of reading sid.

One possible Visualforce page follows:

Class:

public with sharing class myController {
    public String getSalesforceUrl() {
        return System.Url.getSalesforceBaseUrl().toExternalForm();
    }
    public String getSessionId() {
        return UserInfo.getSessionId();
    }
}

Page:

<apex:page controller="myController">
<script>
function getSalesforceUrl() {
    return '{!JSENCODE(salesforceUrl)}';
}
function getSessionId() {
    return '{!JSENCODE(sessionId)}';
}
</script>
</apex:page>

Have your extension load the page and call window.getSalesforceUrl() and window.getSessionId().


Getting the correct host from a window reference:

var sfHost = window.location.hostname;

Getting the SID from a window reference:

var sid = window.contentDocument.cookie.match(/sid=(.+?);/)[1];
7
  • sorry but i dont have access to visualforce.i can only access browser side html and javascript. – user133421 Oct 22 '13 at 2:25
  • yes i called method window.getSalesforceUrl() but getting error Object [object global] has no method 'getSalesforceUrl' – user133421 Oct 22 '13 at 2:27
  • That only works if you have the Visualforce page I mentioned. Can you call window.location.host to get the host of the page? That should suffice for calling the API. – sfdcfox Oct 22 '13 at 2:28
  • yes it would be suffice if the user have no domain in my case my org has a domain and window.location.host gives me "mydevorg-dev-ed.my.salesforce.com" but my instance_name is ap1.salesforce.com how to get this ap1 . – user133421 Oct 22 '13 at 2:30
  • It doesn't matter-- you can use mydevorg-dev-ed.my.salesforce.com as well as you can ap1.salesforce.com. – sfdcfox Oct 22 '13 at 2:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.