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.

  • Userinfo.getOrganizationId() might help. One time process keep this track in some sheet and next time you can easily find. 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. 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

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());
  • 1
    Pity the SandboxContext class is not documented... It's unclear how to pull the name from that class based purely on what you have shared here.
    – Adrian Larson
    Jun 30 '16 at 3:38
  • SandboxPostCopy interface is documented Jun 30 '16 at 9:38
  • 1
    That is an entirely different class. @Vadim Luckily the example contains the method needed to answer this question.
    – Adrian Larson
    Jun 30 '16 at 17:19
  • You're not missing much in SandboxContext: ID organizationId();, ID sandboxId();, String sandboxName(); is all you get May 9 '17 at 20:59

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.

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.