Many of having trouble to find sandbox name from your Sandbox org. There is no direct way to get it but we have workaround. Please follow below steps
- Create list custom setting in your production org with Key and Value Pair like below
- Deploy below apex class code in to Production along with test class
global class SandBoxPostCopyExecution implements SandboxPostCopy {
globalvoidrunApexClass(SandboxContextcontext) {
String strSBName = context.sandboxName();
updateConfigurationCustomSettingsWithSandboxName(strSBName);}
globalvoidupdateConfigurationCustomSettingsWithSandboxName(StringstrSBName) {
Configuration__c objConfiguration =Configuration__c.getValues(‘Sandbox Name’)
if(objConfiguration ==null) {
objConfiguration = new Configuration__c(Name = ‘Sandbox Name’, Value__c = strSBName);
}
else {
objConfiguration.Value__c= strSBName;
}
upsert objConfiguration;}
}
@isTest class SandBoxPostCopyExecutionTest {
@isTest
staticvoidtestSandboxPostCopyScript() {Test.startTest();
//Make sure you are populating dummy org id, sandbox id and sandbox name below
Test.testSandboxPostCopyScript(
new SandBoxPostCopyExecution(),
org.Id,
sandbox.Id,
‘MySandboxName’
);Test.stopTest();
}
}
- call “SandBoxPostCopyExecution” apex class while refreshing your sandbox from production. Refer below image
- Now after the sandbox refresh you have sandbox name in your sandbox environment custom settings.
Reference Document: