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.



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

Source : Link , Question Author : Anup , Answer Author : Daniel Hoechst

Leave a Comment