how to get sandbox name in apex?

How to get the name of the sandbox in apex? I know how to determine if the environment is sandbox or production. Is there any metadata available in salesforce to find out the name of the sandbox that is being used (through apex)? If possible through SOQL as well?

Answer

Well this may seem like a somewhat simplistic approach but when you create a Sandbox, SF appends the name of the sandbox to the usernames. You could simply query one or more usernames from the User object using SOQL and parse out the common suffix (eg. ‘xxx.com.DEV‘).

Granted this isn’t a sure fire way to get the value but might work in a pinch.

Attribution
Source : Link , Question Author : Sathya , Answer Author : CloudMike

Leave a Comment