How to get the name of the sandbox in apex? I know how to determine if the environment is sandbox or production. Is there any metadata available in salesforce to find out the name of the sandbox that is being used (through apex)? If possible through SOQL as well?

5 Answers 5


There is now an official solution that is out with the Spring' 22 release.
You can use the new getSandboxName() method:

System.Domain d = System.DomainParser.parse(URL.getOrgDomainUrl());

You can get more information about this new method in the Apex Reference Guide here.
There are also so other new and useful methods like getMyDomainName().


Well this may seem like a somewhat simplistic approach but when you create a Sandbox, SF appends the name of the sandbox to the usernames. You could simply query one or more usernames from the User object using SOQL and parse out the common suffix (eg. 'xxx.com.DEV').

Granted this isn't a sure fire way to get the value but might work in a pinch.

  • even i was thinking about this @CloudMike but unfortunately there are some usernames without sandbox names appended. So we cant use this trick
    – Sathya
    Commented Sep 4, 2013 at 5:38
  • I figured as much. Is this for an internal solution or a managed package solution? Commented Sep 4, 2013 at 18:19
  • I have a force.com site with a lot of visualforce pages in that application. there are a lot of dynamic configurations needed based on the sandbox names. As of now am just maintaining it in custom settings and taking care of that. but just wanted to check if there is a direct way to fetch this information from some metadata that is already available in salesforce
    – Sathya
    Commented Sep 4, 2013 at 23:35
  • 1
    Can you use the fixed user name 'Chatter Expert' to do your look up? If you are not doing a managed package, then this user will be in your org and will have username like this in a sandbox: [email protected]
    – cropredy
    Commented Sep 6, 2013 at 1:02
  • thats a good idea too @crop1645 :) but not sure if the client will allow me create a new dummy user for this purpose. But chatter users are free and it shouldnt be a problem. will give a try :)
    – Sathya
    Commented Sep 6, 2013 at 1:39

If you want it for some setup sandbox activities, for example to set it to custom setting, you can use this one:


Once it will be set in your custom setting you can use it elsewhere on any user (with or without sandbox name appended).

  • If you are using SAML, your sandbox url could be configured like:


Next call could be used:

return URL.getSalesforceBaseUrl().getHost().substringBetween('--','.');
  • If we inspect message in the top right corner:

    Sandbox: Full

We could use JS to get sandbox name:

var x = document.getElementsByClassName("subMsg normalImportance");
    if (x.length > 1) {
    alert('Sandbox: ' + x[1].innerHTML);

Unfortunately it's working only from UI.

  • I believe, the best approach is to use custom setting, or wait until salesforce provide sandbox name for class organization. Commented Apr 24, 2016 at 7:03

I think you are just looking for the getHost() method on the System URL class. All sandbox pods are cs*, and all production pods are not.

From the docs:

System.debug('Host: ' + URL.getSalesforceBaseUrl().getHost());

So try

public Boolean isSandbox() {
    return URL.getSalesforceBaseUrl().getHost().left(2).equalsignorecase('cs');
  • This doesn't give you the NAME of the sandbox instance (eg. DEV, QA, TEST, etc.) only whether it's a sandbox or production server. Commented Sep 4, 2013 at 23:29
  • exactly @CloudMike is right. This wont give you the sandbox name , this will just tell you if its sandbox or production.. that too if your sandbox url has something like tapp0 your condition will fail.
    – Sathya
    Commented Sep 4, 2013 at 23:33

You must log in to answer this question.

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