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
NakataNakata 

How can i get the URL ?

Good day All, 

 

May i know how can i get the salesforce site URL ? say if we are in sandbox , it show : https://cs2.salesforce.com

 

if production , it show me https://na7.salesforce.com ?

 

I'm using unlimited version.

 

Thank you !

Best Answer chosen by Admin (Salesforce Developers) 
imuino2imuino2

Try this

 

public static String getDomain () {

        String hostname = ApexPages.currentPage().getHeaders().get('Host');
        String domain = 'https://' + hostname + '/apex';
       
        return domain;
       
    }

 

Ignacio.

 

All Answers

imuino2imuino2

Try this

 

public static String getDomain () {

        String hostname = ApexPages.currentPage().getHeaders().get('Host');
        String domain = 'https://' + hostname + '/apex';
       
        return domain;
       
    }

 

Ignacio.

 

This was selected as the best answer
NakataNakata

Cool ....Thank you !

 

Sir, can we use Site object to get that ?

imuino2imuino2

Just use Site.getCurrentSiteUrl() method to do that on a site.

 

Ignacio.

NakataNakata
That's what i have try , and i get the null
BulentBulent

site methods only return value if you are in site context.

if you use them and call them when you are logged in to salesforce (not accessing via your public site) you won't get a value (because system wouldn't know what the current site is).

CodeBeeCodeBee

Hi Bulent,

 

What if we really want to know whether current salesforce is using what hostname? i believe Apex.currentPage will get null if the controller is not referring a page,  what method should we call in this case?

 

Thank you !

Bard09Bard09

Bulent,

 

Can you clarify what "site context" means in this case?  If we're calling the Site class via Apex code on our Production or Sandbox sites-- isn't that considered within the site context?

 

If not, I would really appreciate if you could explain when it works/does not work.  Trying to prepare for the subdomain change in two weeks and Apex classes are the last place we've hardcoded it in.  URLFORs and relative paths don't work in Apex :)

BulentBulent

site context means you page is called via the site url (sandbox or production).

if you call the same page when you are logged in to salesforce all the site methods would return null since your page is not called from your site

NakataNakata

Hi Bulent, 

 

from your opinion, what is the best way to get Site URL ? i need to differential whether we are in production or development environment from coding wise.

 

Thank you !

BulentBulent

Site.getCurrentSiteUrl() is the way. i you are not testing it via sites then it'll return null.

NakataNakata

Thanks Bulent, 

 

If we have a web service class, means it is not under sites so we will get null in this case ? how can we handle if that's happened ?

 

 

Bard09Bard09

Hi Bulent,

 

I guess the null situation is where I'm stuck.  I'm running 100% Apex code via a trigger & class, yet the function you mentioned is returning null when I test on our Sandbox.  Given that I'm working 100% with Apex code, how can I grab the correct domain for our Force.com URL?

 

Thanks for any assistance you can provide.  Only a few more days until D-Day and need to be able to use the correct URLs!

BulentBulent

the correct url would change based on which site is requesting the page. Each site url can be masked with a custom url).

if the call is not made in site context there is no site url to return.

 

Bard09Bard09

Are there any workarounds-- anything?

 

Like I said, right now we've manually coded in our server instance (in 2-3 places), something like: https://na3.salesforce.com/ + RelevantId.  This isn't always used (usually we can use relative links or PageReference URLs) but in some cases it is unavoidable.

 

I can manually update this code when our rollover date happens, but what I'd like to do is replace these hard links with something that don't break if our salesforce server changes.

 

Any ideas?

samrat.1985@lntinfotechsamrat.1985@lntinfotech

I want to get the url(apex/PageName?Parameters) of my visual force page saved in a variable.

As my requirment is such that i need to append a new parameter on the exsisting set of parameters in the current URL.

Any help??

I am using developers edition