You need to sign in to do that
Don't have an account?
Apex : How to know current environment?
Hello
We want to know in which environment we are when running Apex, more precisely we need to know if we are in Production, or Sandbox A, or Sandbox B, etc..
At the moment, we are looking at Org Id to determine the environment, like this :
We do not like this method because everytime we refresh the Sandbox, the Org IDs of the Sandboxes change and we have to update the code.
I have found this link :
http://salesforceworld4u.blogspot.com/2015/12/how-to-get-sandbox-name-in-apex.html
It says we can look at the suffix of the logged-in user, then we can find out if it is Sandbox or Production.
For example, if logged-in user is john.smith@mycompany.com.pprod , than means environment is pprod Sandbox.
We also do not like this method because we are afraid users can change their username and remove the suffix, so method will not work anymore.
What is the clean way of doing this?
We want to know in which environment we are when running Apex, more precisely we need to know if we are in Production, or Sandbox A, or Sandbox B, etc..
At the moment, we are looking at Org Id to determine the environment, like this :
public static Boolean IsProductionOrg() { // Method to check of environement is Production ORG or not return UserInfo.getOrganizationId() == '00ecqzecqc1IkdlUAC' ? true : false; } /** * This method retun whether this org is a production or a sandbox * @return true/false */ public static Boolean IsPProdOrg() { // Method to check of environement is PPROD Sandbox or not return UserInfo.getOrganizationId() == 'cdscddscdsccsdc' ? true : false; }
We do not like this method because everytime we refresh the Sandbox, the Org IDs of the Sandboxes change and we have to update the code.
I have found this link :
http://salesforceworld4u.blogspot.com/2015/12/how-to-get-sandbox-name-in-apex.html
It says we can look at the suffix of the logged-in user, then we can find out if it is Sandbox or Production.
For example, if logged-in user is john.smith@mycompany.com.pprod , than means environment is pprod Sandbox.
We also do not like this method because we are afraid users can change their username and remove the suffix, so method will not work anymore.
What is the clean way of doing this?
create a label and store the name of env ..
after that youneed to return the label or setting is the IsSandbox is true
getSandboxName returns null if run in production.