Have you ever wanted to run one block of code in a sandbox and another block in the production org? I couldn’t find a built-in Apex function that allowed me to determine whether the code was running in a Production/DE environment or in a sandbox.
Fortunately, I ran across this post that gave enough info to write the following simple function called isProdOrg()
, which returns a Boolean.
public static Boolean isProdOrg () {
// adapted from http://boards.developerforce.com/t5/Apex-Code-Development/Finding-Dynamically-the-URL-of-the-Salesforce-instance/m-p/135708/highlight/true
String orgId = Userinfo.getOrganizationId();
System.debug('Org Id: ' + orgId);
// the fourth character in an OrgId is always a number in production and DE orgs
// in sandbox orgs, it is an uppercase letter
String fourthChar = orgId.substring(3, 4);
return Pattern.matches('[A-Z]', fourthChar);
}
Although I’m not sure this is officially “endorsed” by Salesforce, it hasn’t failed me yet!
Questions?
Have questions? Let us know in a comment below, or contact our team directly. You can also check out our other posts on Apex.
Why people still make use of to read news papers when in this technological
globe the whole thing is presented on web?
Hi,
will this also work with packaged Apex code.
(If someone downloads my app from the Appstore)
Thanks
I don’t know, sorry. The person who originally wrote this post is no longer with us. I suspect that this will still work, but I can’t guarantee it.