Run Script After Sandbox Creation and Refresh

As an Administrator, you will create or refresh sandbox from production very often for various purposes. Typically, Salesforce Administrators go through a series of steps every time a sandbox is created or refreshed.

As an Administrator, you will Create Salesforce Sandbox or refresh from production very often for various purposes. Typically, Salesforce Administrators go through a series of steps every time a sandbox is created or refreshed. In Winter’16, Salesforce introduced this feature and it makes your sandbox environment business ready, automates data manipulation or business logic tasks. This feature provides the ability to auto run an Apex Class once the Sandbox is refreshed /copied.

This feature is highly useful when you copy data from production to a sandbox and if

  1. If you have some sensitive data, then you can scrap or mask them.
  2. Invalidate customer emails and other data to avoid any automation in non-production environments which impact customers during the development phases.
  3. meet the enterprise data standards policy / be data complaint
  4. Protect the customer’s privacy data.

Before introducing this feature, Salesforce Administrators have to do above things manually using data loaders or partially automated with batch jobs. Now there is no need of doing all things manually or through batch jobs. Just you need to include all the logic into an Apex Class.

You can either include that Apex class into a sandbox template if the sandbox of type is partial or full copy. It will be useful in a Full or Partial sandbox since it has existing records and you can invalidate email address.

This feature will be more useful with Partial and Full sandboxes compared to Developer & Developer Pro sandboxes since no data are copied in the Developer and Developer Pro versions.

In this post, we are going to explore how we can use the SandboxPostCopy interface to help automate invalidating all email fields in the system to prevent any production emails accidentally reaching your customers.


Implementing the SandboxPostCopy interface will allow a class to run after a sandbox is created or refreshed. This feature comes in very handy when you create your sandbox environment development ready. You will start by defining an InvalidateEmails class that implements SandboxPostCopy and define the runApexClass function.

global class SandboxPostRefresh_AC implements SandboxPostCopy { 

    global void runApexClass(SandboxContext context) { 







From the above code, we can see that the SandboxPostRefresh .runApexClass definition. The function takes the SandboxContext as a default parameter.

SandboxContext class provides three pieces of information that can be used:

  1. organisationId()
  2. sandboxId ( )
  3. sandboxName( )

Based on the above three information, the post scripts will run by knowing for which sandbox and organization it should be called. Let’s go ahead and write the code for our run method.

global static void run() { 

       //List of all emails from the User object  

        List<User> userEmailList = [select Email from User where = ‘System Administrator’]; 

        for(User uc : userEmailList) 


            uc.Email = uc.Email.replace('=','@'); 

            //to remove appended domain 

            String addedPhrase = ''; 

            uc.Email = uc.Email.remove(addedPhrase); 



        if(userEmailList.size() > 0) 


             Update userEmailList; 



Here, we get the list of email fields from User object whose profile is System Administrator and replace the email addresses with correct email address and update the user object. In the same way, you can do for all other object’s email fields and update them with invalidate email address.


With help of this Sandbox Refresh Apex Class, you can process data, execute script for business logic or create some test data in your created or refreshed sandbox. This feature is very useful when a Salesforce instance is refreshed frequently.

Reference Link:

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.