117

I am looking for the most efficient way of identifying the type of org (Production or Sandbox) via Apex code. URL scraping is not useful to me, since I'd like to get this information inside a trigger where URLs don't make sense.

Thanks!

ca_peterson
  • 22,983
  • 7
  • 69
  • 123
Anup
  • 3,810
  • 6
  • 26
  • 41
  • 1
    If you are checking you'd better be sure your code is going to fully portable, ie it will work in any environment. I'm sure you've considered this already but thought it worth pointing out. –  Oct 07 '12 at 21:42
  • You may want to change the accepted answer to use Daniel Hoechst's solution as it seems more current and useful. – Adrian Larson May 05 '15 at 17:48
  • 1
    Yep, makes sense! – Anup May 05 '15 at 17:49
  • 2
    There is a new answer that doesn't use a query. I'd recommend that as the new accepted answer. https://salesforce.stackexchange.com/a/386611/87 – Daniel Hoechst Jan 24 '23 at 02:44

14 Answers14

148

Update: There is a new method that doesn't use a query. Check out this answer

In Summer '14, (version 31.0), there is a new field available on the Organization object.

SELECT IsSandbox FROM Organization LIMIT 1

From the release notes under New and Change Objects:

The Organization object has the following new read-only fields.

  • InstanceName
  • IsSandbox

You can, for example, create a lazy loader that other classes can use like this:

public Boolean runningInASandbox {
    get {
        if (runningInASandbox == null) {
            runningInASandbox = [SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox;
        }
        return runningInASandbox;
    }
    set;
}

You can then call that as a Boolean value:

if (!runningInASandbox()) {
  Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
  msg.setSubject('An email');
  msg.setToAddresses(new List<String>{siteAdmin.Email});
  msg.setPlainTextBody('A message');
  Messaging.sendEmail(new List<Messaging.SingleEmailMessage>{msg});
}
Daniel Hoechst
  • 25,020
  • 7
  • 79
  • 114
  • 4
    Is this still the best answer? it's such a waste of a SOQL query to have to do it this way. – Scott Morrison Sep 12 '19 at 11:59
  • AFAIK, yes. I haven't found any classes that provide this information. The closest I can find is ConnectApi.OrganizationSettings, but it doesn't look like it has any info about sandbox. – Daniel Hoechst Sep 13 '19 at 17:28
  • Thanks for the confirmation, I keep thinking there has to be a metadata method that's been added by now. I suppose we could create our own custom metadata to track it – Scott Morrison Sep 13 '19 at 18:33
  • 1
    Problem with custom metadata is remembering to flip it once a sandbox refreshes. – Daniel Hoechst Sep 13 '19 at 20:58
  • 1
    Agreed, if you could have a script run on sandbox creation that did the query and populated the metadata that would work. I suppose you could create a class that did that, check for the metadata if it doesn't exist, run the query save the metadata that way the query would only run once for every org instance. – Scott Morrison Sep 16 '19 at 15:12
  • Not sure if it was there at the time of your earlier discussion, but I think the System.Domain class in combination with method getSandboxName() can do it without SOQL and without any risks of SF changing domain names. I created a new answer for it below. – Guy Clairbois Sep 30 '22 at 08:36
  • 1
    @GuyClairbois thanks for adding that answer! I've put a link to it from my answer so others will hopefully see it. – Daniel Hoechst Jan 24 '23 at 02:41
27

It's not perfect but the static method

System.URL.getSalesforceBaseUrl()

Will allow you to scrape the instance name, will that be sufficient to distinguish between production and sandbox?

Daniel Blackhall
  • 7,882
  • 10
  • 50
  • 68
  • 1
    Worth mentioning here that some upcoming critical updates are going to remove Sandbox instance numbers from URLs if My Domain is enabled. – Charles T Apr 21 '18 at 16:26
  • 2
    Url.getSalesforceBaseURL() will no longer be usable in Apex code with a version of 59.0 or above starting in Winter '24. – Ashish Narang Sep 29 '23 at 21:54
24

Michael Farrington recently posted an Apex method for this on his blog: http://www.michaelforce.org/recipeView?id=a0Ga000000Ekp65EAB

This method uses URL.getSalesforceBaseUrl().getHost() which should work in a trigger.

Daniel Hoechst
  • 25,020
  • 7
  • 79
  • 114
9

I do think there's a method that allows this without a SOQL query and without running the risk that URLs change because of domain name changes.

(NOTE: this solution only works for distinguishing between Production and Sandbox. It does not distinguish between Production and Scratch orgs.)

Salesforce has a System.Domain class that has a method getSandboxName() that should keep working for any url change, because it is a documented Salesforce method. It either returns the sandbox name, or null if you are in a production org.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_System_Domain.htm#apex_System_Domain_methods

The utility method would look like this:

public static Boolean runningInASandbox {
    get {
        if (runningInASandbox == null) {
            System.Domain domain = System.DomainParser.parse(URL.getOrgDomainUrl());
            runningInASandbox = domain.getSandboxName()==null?false:true;
        }
        return runningInASandbox;
    }
    set;
} 
Guy Clairbois
  • 10,634
  • 31
  • 49
  • It is not functioning in a scratch org as expected. The method domain.getSandboxName() is returning a null value while the SOQL query is returning "IsSandbox" as true. – Jair Apr 26 '23 at 09:21
  • 1
    @jair Thanks for your comment. That's interesting. So basically a scratch org is seen as a sandbox without a name. I don't see an immediate solution for this, except for reverting back to the query mentioned in Daniel Hoechst's answer. – Guy Clairbois Apr 26 '23 at 14:18
  • Also what about developer orgs, i.e. would they be identified as production as well? – wesaw Sep 09 '23 at 07:17
  • Developer orgs would be identified as non-sandbox as well, since sandboxname will be empty. – Guy Clairbois Sep 13 '23 at 12:11
5

Another method worth mentioning is to change the name of the org in the sandbox after it is created and then you can query for the org name.

For instance, in production your org name ( Setup > Administration > Company Profile > Company Information > Organization Name ) is Acme Inc.

In the sandbox named DEV, if you change the Organization Name to Acme Inc. (DEV) you could then query for the org name and use it as you need within your code.

Organization org = [SELECT Name FROM Organization];

Update: It is also possible to get the org name from the UserInfo class eliminating the need for a query: UserInfo.getOrganizationName();

This name change has an additional benefit, the email messages which are sent out from the org when an unhandled script exception occurs include this org name value in the subject line. This makes it easy for an exception email to be quickly identified as having come from production or a sandbox without resorting to having to look up the OrgId.

Sandbox: Developer script exception from Acme Inc. (DEV) : HelperClassName : Attempt to de-reference a null object

Mark Pond
  • 22,949
  • 2
  • 56
  • 103
2

This works for me.

String baseUrl = URL.getOrgDomainURL().toExternalForm();
String boxName = DomainParser.parse(baseUrl).getSandboxName();
Boolean isProduction = boxName == null;
System.debug(isProduction);
2

Check this salesforce Idea link Determine Sandbox or production from apex

There is workaround for this by hard-coding the OrgId in the apex class

Karanraj
  • 148
  • 1
  • 8
  • Hard-coding the id doesn't work like you'd expect, SFDC modifies your metadata and replaces instances of the production org id with the sandbox. I'd use this exact technique previously until I realized it kept changing my production org id constant. – Ralph Callaway Nov 07 '14 at 19:02
2

In the solutions in search of a problem category, you could use the pod identifier from the OrgId to determine if you are dealing with a sandbox.

string podId = UserInfo.getOrganizationId().substring(3,4);
boolean isSandbox = 'TSRQPONMLKJZVWcefg'.contains(podId);
System.debug('IsSandbox: ' + isSandbox);

Caveat Confector: The big weakness here is that you will need to update the list of know sandbox pods as and when Salesforce brings new ones online (so it might be safer sticking with the other solutions).

Daniel Ballinger
  • 102,288
  • 39
  • 270
  • 594
1

I personally always consider a new table/object containing configuration information. This allows you to build an N tier rollout environment without having to fully understand the vagaries of SF. If you are planning, or have, a DEV>TEST>SYSTEST>PRODUCTION environment then I think my suggestion has merit.

After all, the alternative is to create such a table, but in code. I would propose that the code should be the same in all environments and the configuration information be changed.

Ant Smith
  • 76
  • 4
1

What I was looking for when I came to this question today was how to allow my LWC to know the difference. I came up with this easy check, hopefully can help others that may be looking for the LWC JavaScript solution:

get isProduction() {
    return !(window.location.hostname.includes('sandbox'));
}

Then wherever you need the check in the rest of your JS class, use this.isProduction.

Paul N
  • 1,030
  • 2
  • 10
  • 23
1

Posting for reference if someone is looking to the same with Flows. $Organization.IsSandbox does not exist there to this day,

so the workaround by @gorav on his blog post is

  • Before your Decision "Add a Get Record" to your Flow, (name it "getOrganization") pinging the Organization Object
  • Then in your Decision have {!getOrganization.IsSandbox} Equals False
1

There's an easy way where you don't have to query by using the URL.getOrgDomainUrl()

Boolean isRunningInASandbox = String.valueOf(URL.getOrgDomainUrl()).contains('sandbox');

Works perfectly and neatly for me.

0

The accepted answer requires a DML statement. We can do without DML statements by using the code from Daniel Blackhall's answer like this:

Boolean isSandbox = URL.getSalesforceBaseUrl().getHost().left(2).equalsignorecase('cs');

or with My Domain enabled (where the cs part is not necessarily on the left)

Boolean isSandbox = URL.getSalesforceBaseUrl().getHost().containsignorecase('cs');
Willem Mulder
  • 3,461
  • 27
  • 51
-3

For some reason this didn't work for me. This worked in its place

public static Boolean isSandbox() {

Boolean org = ([SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox);

    if(org){
        return true;
    } else{
        return false;
    }

}


To use the code in another class or trigger:

trigger fancyname on sObjname (after insert, after update){

if(MiscFunctions.isSandbox()){...} else{...}

}