Recently after a major deployment, we took refresh of a sandbox org. One of the developers worked on a change request on the refreshed copy. Unfortunately, he forgot to change Named Credentials, Auth Provider, Remote site setting. After working on the change request, developer started testing the same. This resulted in record creation in third party application through API’s. There were many test records which were created in third party applications and the issue got escalated.
In this blog we will see how to void such problems in future. We can make use of apex class to check if the current ORG is a sandbox or Production. Salesforce has a sObject called Organization. This sObject has a standard field, IsSandbox Which will be true if it is a sandbox. For Production this field will be false. By using this field user determine the ORG and set End-Point URL’s pointing to respective ORG’s.
By doing this, even after sandbox refresh, API calls will either point to respective ORG’s all the time or fail to execute because of mismatch in End-Point URL and Remote site setting URL.
Organization a_ORG = [Select Id, Name, IsSandbox from Organization];
if(a_ORG.IsSandbox){
System.debug('This is Sandbox');
}
else{
System.debug('This is Production');
}
Please note: Organization will always return only one record all the time. So you can return the query in one object or list of objects. You will still get the results.
Resource
Organization – Object Reference for the Salesforce Platform.