When developing on Salesforce, it’s very common to need the current org’s hostname URLs in sandbox and production, such as:
myorg.my.salesforce.commyorg.lightning.force.commyorg--sandbox.my.salesforce.com
Many developers solve this by manually parsing the URL in JavaScript (LWC or Visualforce), or by hardcoding hostnames based on assumptions. While this may work today, it introduces multiple long-term risks.
Why You Should Avoid Hardcoding or JavaScript URL Parsing
There are two major problems with the “DIY” approach:
1. Hardcoded URLs are fragile
Salesforce URLs vary across:
- Production vs Sandbox
- My Domain changes
- Lightning vs Classic
- Future platform changes
Any hardcoded hostname is a future maintenance risk.
2. Client-side parsing is unreliable
Extracting hostnames in LWC or Visualforce using window.location or string manipulation:
- Couples your logic to the current UI context
- Breaks easily when Salesforce changes URL structures
- Becomes messy when supporting multiple environments
Salesforce may change URL formats, but Apex system APIs are contractually stable.
The Official and Safe Solution
Salesforce provides an official Apex class specifically designed for this problem:
This class allows you to accurately and safely generate Salesforce hostnames without guessing or parsing URLs yourself, especially when working with managed packages. For example, like DomainCreator.getVisualforceHostname(packageName), is extremely useful when you need to reference Visualforce pages that belong to a managed package. In such cases, the correct hostname is namespace-aware and may vary across environments, making hardcoded or client-side solutions unreliable.
Some Example to Get the Correct Salesforce Hostnames in Apex
String orgDomainHostname = DomainCreator.getOrgMyDomainHostname();
//Get the org's domain hostname url, like for REST API, you will get like: myorg.my.salesforce.com
String lightningHostname = DomainCreator.getLightningHostname();
//Get the org’s lightning hostname url, you will get like: myorg.lightning.force.com
String vfHostname = DomainCreator.getVisualforceHostname();
//Get the org’s visualforce hostname url, you will get like: myorg.vf.force.com
String setupHostname = DomainCreator.getSetupHostname();
//Get the org’s setup domain hostname url, you will get like: myorg.my.salesforce-setup.com
String sfSiteHostname = DomainCreator.getSalesforceSitesHostname();
//Get the org’s Salesforce Sites hostname url, you will get like: myorg.my.salesforce-sites.com. If Salesforce Sites aren’t enabled, this method throws an InvalidParameterValueException.
String expCloudSiteHostname = DomainCreator.getExperienceCloudSitesHostname();
//Get the org’s Experience Cloud sites hostname url, you will get like: myorg.my.site.com. If Digital Experiences aren’t enabled, this method throws an InvalidParameterValueException.
String contentHostname = DomainCreator.getContentHostname();
//Get the org’s content (such as files) hostname url, you will get like: myorg.file.force.com.
These methods return fully qualified, environment-aware hostnames, automatically handling:
- My Domain
- Sandbox suffixes
- Correct Salesforce infrastructure routing
Final Thoughts
If you find yourself:
- Using
window.location.hostname - Hardcoding
mycompany.my.salesforce.com - Writing custom logic to “detect” sandbox vs production
It’s a strong sign that you should switch to System.DomainCreator.
Salesforce already solved this problem, we just need to use the right tool!
Side Notes
- Some legacy apex classes may still rely on URL.getSalesforceBaseUrl().toExternalForm() to get the base url, but this method is deprecated API version 59.0 and later. Check it and replace as soon as possible.
- To replace URL.getSalesforceBaseUrl(), Salesforce recommends using getOrgDomainUrl() to get the canonical URL for your org or use getCurrentRequestUrl() to get the URL of an entire request on a Salesforce instance.
URL.getOrgDomainUrl().toExternalForm()returns a simple org base URL, whileDomainCreatoris more comprehensive, handling Visualforce, managed packages, Lightning, and Experience Cloud; use the former for basic URLs, and the latter for reliable, cross-environment URL generation.
Reference Links
Get Org-Specific Domain Hostnames and Information