I have registered my domain in Salesforce org
I wanted to know how could I get domain name using apex.

Suppose my domain is XXXX-dev-ed, I know we have URL.getSalesforceBaseUrl. but what I am looking for is

  1. Which method would return me my domain name such that I could add condition in my method like

       //Append Doamin Name and construct URL
       //construct URL using SF instance and by using URL methods.
  2. How to check if user has register a Domain in SF org?

I checked System.URL.getSalesforceBaseUrl().getHost() but that returned me


I could add parsing logic for string to just get XXXX-dev-ed from above string like System.URL.getSalesforceBaseURL().getHost().split('--c.')[0] , but problem will occure if user has not registered any custom domain.

please advice if there is any best way available to directly get registered SF domain name and way to check if user has registered a custom domain

I believe Salesforce must be storing this information somewhere not sure if can access this using SOQL or by using some other-way.

I want to construct URL when user has registered a Domain and when user has not registered.

Custom Domain --> enter image description here


you can run SOQL on organization object which will return you instance name

Select InstanceName from Organization

To get domain details you can query Domain object

SELECT Domain FROM Domain


  • Customer Portal users can’t access this object.
  • To view this object, you must have the “View Setup and Configuration” permission.
  • Site.com Publisher users have read-only API access to the Domain and DomainSite objects.
  • Class should be defined as without sharing keyword.
  • 1
    that will give me value like NA31,na11 or whatever the SF instance available, I want to find registered domain name. Jan 4 '18 at 7:29
  • for that you can query domain object
    – Himanshu
    Jan 4 '18 at 7:31
  • 3
    SELECT Domain FROM Domain will return domain registered for your Froce.com site, is there any way using which I can retrieve my custom Domain Jan 4 '18 at 9:47
  • 1
    I want information regarding how we can retrieve our custom domain name ! Jan 4 '18 at 9:51
  • Also how we can check if user has registered custom domain Name. Jan 4 '18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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