Find your content:

Search form

You are here

Can we determine if the Salesforce instance is production org or a Sandbox org?


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.


Attribution to: Anup

Possible Suggestion/Solution #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.

Attribution to: Ant Smith

Possible Suggestion/Solution #2

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?

Attribution to: Daniel Blackhall

Possible Suggestion/Solution #3

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

Attribution to: Karanraj

Possible Suggestion/Solution #4

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

Attribution to: Mark Pond

Possible Suggestion/Solution #5

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

Attribution to: Daniel Ballinger

Possible Suggestion/Solution #6

Michael Farrington recently posted an Apex method for this on his blog:

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

Attribution to: Daniel Hoechst

Possible Suggestion/Solution #7

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');

Attribution to: Willem Mulder

Possible Suggestion/Solution #8

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});

Attribution to: Daniel Hoechst
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content