+ Start a Discussion
JosherPackJosherPack 

SandboxPostScript - Help with Test

Use Case: I have users that need to be admins in the sandbox, but not in production.  I have written a SandboxPostCopy apex class to:
  1. Make them active in the sandbox
  2. Update their Profile to System Administrator
  3. Change their Sandbox email to their real email
I got the class, and the test to work, when I hard coded individuals names and emails.  I am writing this new class to do the same thing by simply checking a custom field I created on their user account called Make Sandbox Admin.

My class and test are below.  The test passes, but shows 0% code coverage.  As I am new to apex development, any help with this will be greatly appreciated.


global class AfterSandboxRefresh implements SandboxPostCopy {
  global void runApexClass(SandboxContext context) {

    //Pull System Administrator Profile
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];       
        
  //Pull Users to Make Sandbox Admin
        List<User> listOfUsers = new List<User>();

    List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
                    FROM User
                   WHERE Make_Sandbox_Admin__c = True];

    For (User u : UsersToUpdate) {
      u.ProfileId = p.Id;
            u.isActive  = True;
                String e1 = u.email;
              String e2 = e1.remove('@example.com');
              String target = '=';
              String replacement = '@';
              String e3 = e2.replace(target, replacement);
            u.Email = e3;
      listOfUsers.add(u);            
    }
  Update(listOfUsers);
    }
}




@isTest
class AfterSandboxRefreshTest
{
  @isTest
  static void testSandboxPostCopyScript() 
  {
  
    //Pull System Administrator Profile
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];       
        
  //Pull Users to Make Sandbox Admin
        List<User> listOfUsers = new List<User>();

    List<User> UsersToUpdate = [SELECT Id,Email, FederationIdentifier
                    FROM User
                   WHERE Make_Sandbox_Admin__c = True];

    For (User u : UsersToUpdate) {
      u.ProfileId = p.Id;
            u.isActive  = True;
                String e1 = u.email;
              String e2 = e1.remove('@example.com');
              String target = '=';
              String replacement = '@';
              String e3 = e2.replace(target, replacement);
            u.Email = e3;
      listOfUsers.add(u);            
    }
  Update(listOfUsers);
    
    
    Test.startTest();
    
      // Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
      // Id possible pass valid id
      Test.testSandboxPostCopyScript(
        new SandboxRefresh(), 
        '00Dj0000001srun', 
        '00Dm00000008k1D', 
        'MySandboxName'
      ); 
      
    Test.stopTest();
    
  }
}
 
Best Answer chosen by JosherPack
Amit Chaudhary 8Amit Chaudhary 8
Please take help from below post
1) https://developer.salesforce.com/forums/?id=906F0000000kAQfIAM
Please try below code
@isTest
class AfterSandboxRefreshTest
{
  @isTest
  static void testSandboxPostCopyScript() 
  {
  
	Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];       
			
	User u = new User(Alias = 'standt', Email='Test00Dj0000001srun@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='Test00Dj0000001srun@testorg.com' , Make_Sandbox_Admin__c = True );
    insert u;
	
    
    Test.startTest();
    
		// Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
		// Id possible pass valid id
		Test.testSandboxPostCopyScript(
			new AfterSandboxRefresh(), 
			'00Dj0000001srun', 
			'00Dm00000008k1D', 
			'MySandboxName'
		); 
      
    Test.stopTest();
    
  }
}

Let us know if this will help you

Thanks
Amit Chadhary

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please take help from below post
1) https://developer.salesforce.com/forums/?id=906F0000000kAQfIAM
Please try below code
@isTest
class AfterSandboxRefreshTest
{
  @isTest
  static void testSandboxPostCopyScript() 
  {
  
	Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];       
			
	User u = new User(Alias = 'standt', Email='Test00Dj0000001srun@testorg.com', 
		EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
		LocaleSidKey='en_US', ProfileId = p.Id, 
		TimeZoneSidKey='America/Los_Angeles', UserName='Test00Dj0000001srun@testorg.com' , Make_Sandbox_Admin__c = True );
    insert u;
	
    
    Test.startTest();
    
		// Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
		// Id possible pass valid id
		Test.testSandboxPostCopyScript(
			new AfterSandboxRefresh(), 
			'00Dj0000001srun', 
			'00Dm00000008k1D', 
			'MySandboxName'
		); 
      
    Test.stopTest();
    
  }
}

Let us know if this will help you

Thanks
Amit Chadhary
This was selected as the best answer
JosherPackJosherPack
That worked perfectly, Amit.  Thanks for your help.
Kimball RobinsonKimball Robinson
See my answer here: https://salesforce.stackexchange.com/a/240183/25165