+ Start a Discussion
narsavagepnarsavagep 

Testing for Sandbox Environment?

Is there a way for the code to know whether or not it is running in a production instance, or a sandbox?

 

I have scheduled APEX that sends an email to specific email addresses three times a day.  Whenever someone makes a new sandbox in our company, that sandbox starts sending the emails and I have to manually go into every sandbox when it is created and remove the scheduled APEX.  I want the function to be smart enough to know that it is running with a sandbox environment, and therefore not send the email.

 

Any ideas?  

 

Thanks!

Paul

Best Answer chosen by Admin (Salesforce Developers) 
narsavagepnarsavagep

After some research and looking at the "Ideas" board... I found a solution.

 

I wrote this and put in my global variables class, it does exactly what I need:
 
public static boolean IsProduction() {
return ( UserInfo.getOrganizationId() == 'XXXX' );
}
 
(Just replace the XXXX with your Production Organization Id.)

 

 

Hope this may help someone else out there with similar question.

Paul

All Answers

narsavagepnarsavagep

After some research and looking at the "Ideas" board... I found a solution.

 

I wrote this and put in my global variables class, it does exactly what I need:
 
public static boolean IsProduction() {
return ( UserInfo.getOrganizationId() == 'XXXX' );
}
 
(Just replace the XXXX with your Production Organization Id.)

 

 

Hope this may help someone else out there with similar question.

Paul

This was selected as the best answer
michaelforcemichaelforce

I have posted a more generic solution for this on my blog: http://www.michaelforce.org/recipeView?id=a0Ga000000Ekp65EAB

 

By "generic" I mean it is good if you plan to share the code with multiple orgs through packaging... using my class will work no matter where you are!

 

Hope this helps anyone finding this thread through search!