How to check Sandbox or Production using Apex?

How to check Sandbox or Production using Apex?

Option 1:

Sample SOQL:

SELECT Id, IsSandbox, Name FROM Organization LIMIT 1

If IsSandbox is true, then it is Sandbox else it is Production.

Option 2:

Sample Apex Class:

public class Utility {

    public static Boolean sandboxBool;
    
    public static Boolean isSandbox() {
        if ( sandboxBool == null ) 
        sandboxBool = URL.getSalesforceBaseUrl().getHost().left(2).equalsignorecase('cs');
        return sandboxBool;
    }

}

Leave a Reply