This question already has an answer here:

I want to access custom my domain in apex class. For e.g https://checkDB.my.salesforce.com is the domain. Is there any method to access this domain in class.

marked as duplicate by Tushar Sharma, Basti, Oleksandr Berehovskyi, Gortonington, Martin Lezer Jan 18 '18 at 13:58

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.


Following statement will give you your base URL.

System.debug('URL ' + System.Url.getSalesforceBaseUrl().toExternalForm() );

Update: Added replacement logic for VF.

   public Pagereference getData(){
        String str=  System.Url.getSalesforceBaseUrl().toExternalForm() ; 
        System.debug('removing -- : ' + str.substringBeforeLast('--') );
        str = str.substringBeforeLast('--') + '.my.salesforce.com';
        System.debug('str: ' + str);
        return NULL;
  • It returns checkDB--c.tes.visual.force.com – s.cloud Jan 18 '18 at 11:23
  • not exactly same domain name checkDB.my.salesforce.com – s.cloud Jan 18 '18 at 11:23
  • Yes it does returns same. Where you're testing the same? I tried this in Anonymous window. (you can try the same). – Ysr Shk Jan 18 '18 at 11:51
  • True, while running on Anonymous window getting https:// checkDB.my.salesforce.com. I have created page and class, writing this line in method. <apex:commandButton action="{!getData}" value="Submit" > Class public Pagereference getData(){System.debug('URL ' + System.Url.getSalesforceBaseUrl().toExternalForm() ); } and Preview the page – s.cloud Jan 18 '18 at 14:33
  • 1
    Please be aware that your method for parsing a Visualforce base URL should be built to remain compatible with the following upcoming Critical Update: releasenotes.docs.salesforce.com/en-us/spring18/release-notes/… so for example mydomain--c.na5.visual.force.com would become mydomain--c.visualforce.com. – Charles T Jan 18 '18 at 15:35

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