12

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?

7

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.

| improve this answer | |
  • even i was thinking about this @CloudMike but unfortunately there are some usernames without sandbox names appended. So we cant use this trick – Sathya Sep 4 '13 at 5:38
  • I figured as much. Is this for an internal solution or a managed package solution? – CloudMike Sep 4 '13 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 Sep 4 '13 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: chatty.somelonguniquifier@chatter.salesforce.com.YOURSANDBOXNAME – cropredy Sep 6 '13 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 Sep 6 '13 at 1:39
5

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

UserInfo.getUserName().substringAfterLast('.');

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

| improve this answer | |
2
  • If you are using SAML, your sandbox url could be configured like:

    https://sbnamesso--sbname.cs41.my.salesforce.com

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.

| improve this answer | |
  • I believe, the best approach is to use custom setting, or wait until salesforce provide sandbox name for class organization. – Ilya Lepesh Apr 24 '16 at 7:03
0

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');
}
| improve this answer | |
  • 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. – CloudMike Sep 4 '13 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 Sep 4 '13 at 23:33

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.