You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Run Script After Sandbox Creation and Refresh


By: Santhosh

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.

Implementation 

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) { 

        System.debug(context.organizationId()); 

        System.debug(context.sandboxId()); 

        System.debug(context.sandboxName()); 

        run(); 

    } 

} 

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 profile.name = ‘System Administrator’]; 

        for(User uc : userEmailList) 

        { 

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

            //to remove appended domain 

            String addedPhrase = '@example.com'; 

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

            userEmailList.add(uc); 

        } 

        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.

CONCLUSION 

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: 

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