Option 1:
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;
}
}
Controller/Trigger calling:
system.debug(‘ o ‘ + Utility.isSandbox());
Option 2:
Sample SOQL:
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;
}
}
Controller/Trigger calling:
system.debug(‘ o ‘ + Utility.isSandbox());
Option 2:
Sample SOQL:
SELECT Id, IsSandbox, Name FROM Organization LIMIT 1
If IsSandbox is true, then it is Sandbox else it is Production.