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.


  • 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. – user684 Oct 7 '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 5 '15 at 17:48
  • 1
    Yep, makes sense! – Anup May 5 '15 at 17:49

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;

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});
| improve this answer | |
  • 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
  • Problem with custom metadata is remembering to flip it once a sandbox refreshes. – Daniel Hoechst Sep 13 '19 at 20:58
  • 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

It's not perfect but the static method


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

| improve this answer | |
  • Even though this is a not too bad way of doing it, the new way as described in latest answer given to this question is better. Just a heads up to anyone reading this question and not looking further than the accepted answer. – rael_kid Nov 28 '14 at 10:48
  • 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

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.

| improve this answer | |
  • 4
    This is an exact duplicate of my answer – Daniel Blackhall Aug 3 '12 at 1:29
  • 2
    I tend to prefer using the orgID by principle since ID's are less prone to be modified for various reasons. – ebt Aug 6 '12 at 13:55
  • 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

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

| improve this answer | |

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

| improve this answer | |
  • 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 7 '14 at 19:02

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

| improve this answer | |

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.

| improve this answer | |

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');
| improve this answer | |
  • 2
    That approach doesn't work for any org which has MyDomain enabled. – Adrian Larson Apr 21 '18 at 15:44
  • @AdrianLarson Interesting. That appears not to be true in my tests. The domain will look something like 'subdomain.cs88.my.salesforce.com'. Is that not the case in your situation? – Willem Mulder Apr 21 '18 at 17:12
  • And .left(2) gets you 'cs'? – Adrian Larson Apr 21 '18 at 17:19
  • Good catch :-D I've added a .containsignorecase() variant. Thanks. – Willem Mulder Apr 23 '18 at 7:37

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.