I need to return this value:

enter image description here

Is there anyway to get the registered Force.com domain for a given org, with raw Apex code or Metadata API?

  • Clarification: The objective is to obtain the configured Force.com Sites domain (this is the domain that is set once via Setup / Develop / Sites, and cannot be changed). We want to know: a) if such a domain has been configured already, and b) if there's a Force.com Sites domain configured, its URL. And we want to do so, even if no Force.com Sites exist in the org. The ultimate objective is to do the following from a custom settings page: 1) Guide the user in the right direction if no domain has been set, and 2) create a Force.com site (we intend to use the Metadata API). Hope this helps. Sep 29, 2016 at 13:32

2 Answers 2


You just need SOQL/SOSL on the Domain object. Just to double check, I looked at a domain, saw a url of instance.salesforce.com/0I4?parameters=values, and ran:

system.debug(Id.valueOf('0I4000000000000').getSObjectType()); // yields Domain

The Domain Name column you have highlighted is represented by the Domain field, leading to a somewhat silly looking query:

SELECT Domain FROM Domain

Query Results

I have also found that with any Site records configured, you get one record in that list view that is not a domain at all, but a link to your Site tab. The url you land on if you follow the link will look like instance.salesforce.com/0DM?parameters=values. If you want the value of the "Domain Name" from that column, you have to get a bit more hack-y. I modified the code posted there by @mattandneil just a bit to get the exact value:

Site site = [SELECT GuestUserId FROM Site WHERE UrlPathPrefix != null LIMIT 1];

String path = '/sites/servlet.SiteDebugMode';
PageReference pr = new PageReference(path);
pr.getParameters().put('sitedebugmode', 'x');
pr.getParameters().put('guid', site.GuestUserId);
String url = pr.getContent().toString().substringAfter('URL=').substringBefore(path);

system.debug(url.substringBetween('//', '/'));

I do not believe this behavior is supported, but it has worked for at least two years, and does so currently.

  • Great answer, but I'm not able to retrieve Domains with that SOQL. Do I need an special permission, I'm on a dev org. Sep 27, 2016 at 18:57
  • @MiguelLattuada Are you able to run the query? Can you be more specific? I haven't been able to hunt down any permissions related to Domain after a bit of digging.
    – Adrian Larson
    Sep 27, 2016 at 18:58
  • I'm not getting any error, It's just an empty result. Sep 27, 2016 at 19:02
  • @MiguelLattuada See my updates.
    – Adrian Larson
    Sep 28, 2016 at 18:24
  • I get an error while running this as a non-admin user
    – Phil B
    Dec 8, 2016 at 18:54

you can store this in custom setting. Although if you still need this in that case you can take help from URL class


or you can query Domain sObject

Select id,Domain from domain
  • 1
    I don't see what the base url has to do with it? Or why you deleted your answer!
    – Adrian Larson
    Sep 27, 2016 at 16:54
  • @AdrianLarson yes I want to ask you that I have one custom domain in my org not released for user. When I run this query I get 0 count so not sure if this is the correct answer. Sep 27, 2016 at 16:56
  • Ah yeah I had one with visibility issues as well, but it was CreatedBy someone else. Hmm...
    – Adrian Larson
    Sep 27, 2016 at 17:02
  • 1
    It's surely the correct object. Not sure why visibility would be an issue.
    – Adrian Larson
    Sep 27, 2016 at 17:10
  • 1
    @AdrianLarson This isn't working for me either. Running as a System Administrator, with Force.com sites enabled and the View System Configuration permission (of course, it's the default Sys Admin profile, but just to be clear). I have a Force.com Site created, and a domain set for the Force.com Sites, which is listed under My Domains. Now ... A query on the Site object returns the Force.com site, with a correct value on the SiteDomain field. A query on the Domain object returns no results. A query on the DomainSite object returns no results either. Any idea what could be going on here? Sep 28, 2016 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .