As per this salesforce help document, the content files base URL is different compare to the salesforce org domain URL.

The domain URL formats with or without enhanced domains are as follows

//with enhanced domains
Login: MyDomainName.my.salesforce.com 
VF page: MyDomainName--PackageName.vf.force.com 
Content(files): MyDomainName.file.force.com

//without enhanced domains
Login: MyDomainName.my.salesforce.com 
VF page: MyDomainName--PackageName.visualforce.com 
Content(files): MyDomainName--c.documentforce.com

In the VF page context, we can do mapping of the VF page URL i.e Url.getSalesforceBaseUrl() and Content Page URL, and get the content URL. But in the case of Webservice context URL.getOrgDomainUrl() or Url.getSalesforceBaseUrl() is the same with or without enhanced domains. So we cant keep mapping and get content files URL via apex dynamically in apex.

How to find out content files URL using Url.getSalesforceBaseUrl() OR URL.getOrgDomainUrl() through apex?


2 Answers 2


You can use the DomainCreator class for this. It will return the correct URL no matter if the enhanced domains were enabled or not:


The best practice, assuming you're working within the Salesforce web app for an authenticated user, is to leave off the first part of the URL and to include only the other parts of the URL. Salesforce will figure out the rest.

So, for example, if your file is found at a given URL, just remove the host and use a relative URL. That's also a way to handle Visualforce pages in formulas - just start with "/apex/MyPageName" and ignore the first part.

  • When I use relative URL, the app internally redirects to document force URL to serve image file, this consuming 2 browser requests for every image load. The first URL always fails with 301 moved permanently status. On the web app home page, around 30+ images are displayed and the extra requests slowing page performance. Commented Dec 6, 2021 at 9:36

You must log in to answer this question.

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