Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Salesforce by (11.9k points)
recategorized by

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 ?

1 Answer

+1 vote
by (32.1k points)
edited by

To retrieve your own organization’s Salesforce instance’s URL, and not the visual force URL, you can use this function:

Want to learn Salesforce from basics! Here's the right video for you on Salesforce provided by Intellipaat:

System.URL.getOrgDomainUrl().toExternalForm()

Let’s understand what this function does:

System.URL

Methods of this class can be used to create links to objects in your Org.

getOrgDomainUrl()

Regardless of the context, this method returns the same domain for your Org.

toExternalForm()

A string representation of the current URL can be returned by this method.

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce Training

Browse Categories

...