Using the SandboxPostCopy Interface to Update System Admin Email Addresses


When creating or refreshing a Sandbox, Salesforce automatically makes alterations to the Users’ email addresses, to prevent automated emails from being inadvertently sent to individuals.  System Administrators however, often need to be exempted from this update to their User records.

 

Typically, a System Administrator wishing to have access to a shared sandbox (UAT or QA, for example) contacts the individual who initiated the refresh/creation to have their email address in that environment manually updated.

 

By leveraging the SandboxPostCopy interface, we can automate this update to keep project velocity from slowing due to the wait time to have the value manually changed.

 

The interface itself is very simple, having only a single method (runApexClass), which is executed automatically by the system after the refresh is complete.

 

The runApexClass method takes a single SandboxContext variable, but for the purposes of updating the email addresses of System Administrators, it is not used.

 

When writing the test class for the implementation class of SandboxPostCopy, use the @TestSetup annotation and have your setup method query for, and update all the email addresses for System Administrator Users:

 

List sysAdminList = [
        SELECT Id, Email
        FROM
User
        WHERE Profile.Name = 'System Administrator' AND IsActive = true
];

for(User sysAdmin : sysAdminList) {
    String email = sysAdmin.Email;
    email = email.replace('@', '=');
    email += '@example.com';
    sysAdmin.Email = email;
}

update sysAdminList;

 

Your actual testmethod should create a SandboxContext variable, which is then passed to the runApexClass method in your implementation class:

SandboxContext sc;
SandboxPostCopyImpl impl = new SandboxPostCopyImpl();
impl.runApexClass(sc);

 

Use the Test.stopTest() method to ensure that the update has completed, then loop over the same query used in the setup method and assert that each email address does not end with ‘@example.com’:

 

for(User sysAdmin : sysAdminList) {
    System.assert(!sysAdmin.Email.endsWith('@example.com'));
}

 

When it comes time for a new sandbox to be created, or for a refresh of an existing sandbox, specify the name of your implementation class in the Sandbox Options screen prior to clicking the create button:

 

Salesforce B2B Commerce Cloud Sandbox Operations

 

SEARCH ARTICLES