function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jesus GJesus G 

Test class SandboxPostCopy Interface

Hello!

Please, could you help me to write the test class for this code where I am using the SandboxPostCopy interface to modify an account field after refreshing a sandbox? I have tried by creating a test class that updates Account records but the code coverage is 0...
global class AfterSandboxRefresh implements SandboxPostCopy {
	global void runApexClass(SandboxContext context) {

	List<Account> listOfAccounts = new List<Account>();

	List<Account> accountsToUpdate = [SELECT Id, Email__c
									  FROM Account
									  WHERE Email__c != NULL AND Email__c Like '%_@__%.__%'];

	if(!accountsToUpdate.isEmpty()) { 
		For (Account acc : accountsToUpdate) {
			Email__c = Email__c + '.noemail';
			listOfAccounts.add(acc);            
		}
	Database.Update(listOfAccounts, false);
	}
	}
}

Thank you very much!
J
Best Answer chosen by Jesus G
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest
class AfterSandboxRefreshTest
{
	@isTest
	static void testSandboxPostCopyScript() 
	{
	
		Account acc = new Account();
		acc.Name ='Test';
		acc.Email__C = 'Test@test.com';
		insert acc;
		
		
		Test.startTest();
		
			// Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
			// Id possible pass valid id
			Test.testSandboxPostCopyScript(
				new AfterSandboxRefresh(), 
				org.Id, 
				sandbox.Id, 
				'MySandboxName'
			); 
			
		Test.stopTest();
		
	}
}
Please let us know if this will help you

Thanks
Amit Chaudhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class.
@isTest
class AfterSandboxRefreshTest
{
	@isTest
	static void testSandboxPostCopyScript() 
	{
	
		Account acc = new Account();
		acc.Name ='Test';
		acc.Email__C = 'Test@test.com';
		insert acc;
		
		
		Test.startTest();
		
			// Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
			// Id possible pass valid id
			Test.testSandboxPostCopyScript(
				new AfterSandboxRefresh(), 
				org.Id, 
				sandbox.Id, 
				'MySandboxName'
			); 
			
		Test.stopTest();
		
	}
}
Please let us know if this will help you

Thanks
Amit Chaudhary
This was selected as the best answer
Jesus GJesus G
Hello Amit,

Many thanks for your reply! It worked perfectly :)

I already checked the documentation and I was trying something very similar but I was not specifying the parameters properly.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_interface_System_SandboxPostCopy.htm

Thank you,
J
Deepu B 5Deepu B 5
Hi,
Can anyone please help on how can we execute the apex class in anonymous window.I am also writing similar apex class.Thanks