This question already has an answer here:

I have created one site page. I send a link to this page to some users in an email. Currently, the site page is in a sandbox and I have hard coded this link in email. This means when I move this to production, I'll need to update the hard-coded link in the email. I'd like to avoid hard coding this link. Is there a dynamic method to determine the URL for a site page?

marked as duplicate by tomlogic, Samuel De Rycke, Mike Chale, Ralph Callaway, Jesse Altman Feb 19 '13 at 15:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You can query on the Site object and construct the URL, for example, HTTPS:

Site site = [SELECT Subdomain, UrlPathPrefix FROM Site];
String url = 'https://' + site.Subdomain + '.secure.force.com/' + site.UrlPathPrefix;

/* https://myname.secure.force.com/mysite */
  • 1
    Wow, I didn't know you could query the Site like that. – Phil B Feb 19 '13 at 15:16
  • its good but i am not getting correct result. – Pankaj Feb 20 '13 at 5:35

I would never hard code a value that changes per environment. As @PJC said, this isn't a consistently easy thing to do in Apex. I would suggest looking into Custom Settings. From the documentation:

Custom settings are similar to custom objects and enable application developers to create custom sets of data, as well as create and associate custom data for an organization, profile, or specific user. All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. This data can then be used by formula fields, validation rules, Apex, and the SOAP API.

So, you can create a Custom Setting, let's call it Force_com_Site_Url__c and it has a URL field called Site_Url__c. To access that, it would be

Force_com_Site_Url__c siteSettings = Force_com_Site_Url__c.getInstance();
System.debug('This is the URL - ' + siteSettings.Site_Url__c);

This doesn't count against any database limits and all of this information is cached so it returns very quickly. It is meant to act like a property file in other programming environments.


To give you a simple answer, you can't, however, with some tinkering you can get it to work, it's not clean though. Else I would recommend just editing your email template on your production environment.

Please check the following link, I asked a potential helpful question a few weeks ago: Get Site URL from apex

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