Home / Blog / Determining Org Type in Apex Code

Determining Org Type in Apex Code

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!


Have questions? Let us know in a comment below, or contact our team directly. You can also check out our other posts on Apex.

3 thoughts on “Determining Org Type in Apex Code”

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

Leave a Comment

Your email address will not be published. Required fields are marked *