Retrieve salesforce instance URL instead of visualforce instance

jmrjulian picture jmrjulian · Feb 20, 2012 · Viewed 38.6k times · Source

From a visualforce page, I need to retrieve our organization's salesforce instance's URL, and not the visual force URL.

For example I need https://cs1.salesforce.com instead of https://c.cs1.visual.force.com

Here's what I've tried so far and the outcome I got:

Accessed the Site global variable from the VF Page:

<apex:outputText value="{!$Site.Domain}" /> returns null

Sidenote: Everything in $Site.xxx seems to return null.

From the Apex controller:

public String getSfInstance()
{
  return ApexPages.currentPage().getHeaders().get('Host');
}

and

public String getSfInstance()
{
  return URL.getSalesforceBaseUrl().toExternalForm();
}

returns c.cs1.visual.force.com and https://c.cs1.visual.force.com, respectively.

Question: How do I retrieve what I want: https://cs1.salesforce.com?

Answer

Shivanath D picture Shivanath D · Dec 10, 2012

Here's something that I used within my Apex Trigger

System.URL.getSalesforceBaseUrl().getHost().remove('-api' );

This gives me proper URL