20

Now after all mere subscribers has per default a lot of sandboxes available, I need a way to programmatically (dynamically) get the sandbox-name of the sandbox org where the actual code is executed.

Just for clarification: I'm not looking for the name of the org, but the name of the sandbox, which are two different things.

One example purpose for this requirement is to tag notification emails, so that it's clear which sandbox-org has created it. For sure I don't want to hardcode it. Also I don't want to use a custom setting.

If found a few unclean approaches like

  • UserInfo.getUserName().substringAfterLast('.'); ==> this is not reliable, username might have been changed or a system-user might act. But this comes closest to what I need.

  • use of Organization.IsSandbox ==> not good enough, does not tell me which of the 25+ possible sandboxes it is. Only that's not production. I need more

  • use of Organization.Name ==> all the same for all sandboxes

  • use of Organization.InstanceName ==> provides the server name like CS83, very nice, but not good enough

I think there is no officially supported way to get it... but I'm not 100% sure...

Is there any approach to "hack" that sandbox name out of the guts of the platform? I need no sample code, just an idea or a concept is good enough for me.

Uwe Heim
  • 28,350
  • 18
  • 115
  • 283
  • Userinfo.getOrganizationId() might help. One time process keep this track in some sheet and next time you can easily find. – Tushar Sharma Jun 26 '16 at 11:59
  • 1
    Thanks @TusharSharma , but the OrganizationId changes on every refresh, so it would be manual work plus error prone. Looking for something 100% dynamic. – Uwe Heim Jun 26 '16 at 12:05
  • Is there any particular reason not to create a custom setting and store the Sandbox's name on it? AFAIK it is not possible to obtain the name programatically the way you want. Every article I can find on the net suggests SOQL on Organization or getting the bits after '.com' on the user's name. – Renato Oliveira Jun 26 '16 at 14:39
  • @Renato that would get wiped every refresh. Custom metadata could work... – Adrian Larson Jun 26 '16 at 15:12
  • 4
    @AdrianLarson wouldn't the metadata revert back to production values too? Either way with custom settings or metadata, after each sandbox refresh the admin needs to go one-time update the value for that sandbox. – Doug Ayers Jun 27 '16 at 15:55
  • @UweHeim, did any of the below answers help out? if so, please mark one as checked, thanks! – glls May 03 '19 at 13:30

4 Answers4

13

After Spring'16 release there is an ability to specify an Apex class which executes every time the sandbox is copied. See SandboxPostCopy Interface. You can get the sandbox name from SandboxContext and store it in a custom setting. Notice the example indicates this method signature exists: SandboxContext.sandboxName().

global class HelloWorld implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        System.debug('Hello Tester Pester ' + context.organizationId() + ' 
                  ' + context.sandboxId() + context.sandboxName());
    }
}
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
Vadim Rudkov
  • 173
  • 7
7

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());
System.debug(d.getSandboxName());

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().

Fabien Taillon
  • 8,605
  • 2
  • 38
  • 58
1

One possible way is screen scraping. Take a look at this answer on how to scrape screen using http request. screen scrape Salesforce with REST GET call from Apex

Please note 2 things

1) This approach seems fragile
2) You can use it from JavaScript and Apex

There is idea posted for for this feature. https://success.salesforce.com/ideaView?id=08730000000Dld9AAC

So, seems like we will have to leave with workarounds mentioned in OP + the one i mentioned.

AtulRajguru9
  • 9,110
  • 3
  • 28
  • 66
0

The DomainParser class is the best way to find the name of the actual sandbox you are working in. (https://help.salesforce.com/s/articleView?id=release-notes.rn_apex_domain_classes.htm&release=236&type=5)

I think there may be some confusion from looking at previous answers...

context.SandboxName() will give you the Organization name as listed in the Company Information section. In my experience this mirrors prod in all sandboxes, and is certainly what I get when I try the above example.

I've tested Fabiens answer above and this will give you the name of the actual sandbox you are in as it is listed in the Sandboxes section in production. This is what I was looking for and I'd imagine its what most people are looking for when they visit this question

IvanTT
  • 3
  • 3