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!
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!
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});
}
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?
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.
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.
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;
}
domain.getSandboxName()
is returning a null value while the SOQL query is returning "IsSandbox" as true.
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
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
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).
This works for me.
String baseUrl = URL.getOrgDomainURL().toExternalForm();
String boxName = DomainParser.parse(baseUrl).getSandboxName();
Boolean isProduction = boxName == null;
System.debug(isProduction);
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.
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
.
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
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.
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');
public static Boolean isSandbox() {
Boolean org = ([SELECT IsSandbox FROM Organization LIMIT 1].IsSandbox);
if(org){
return true;
} else{
return false;
}
}
trigger fancyname on sObjname (after insert, after update){
if(MiscFunctions.isSandbox()){...} else{...}