Run Script After Sandbox Creation

Salesforce added a really nice feature a couple of releases ago, but I started using it a couple of weeks ago, and is really interesting: The ability to run a script when creating or refreshing a sandbox.
The initial post that they published doesn’t have too much information, so I want to share with you what I did, how we are using it nowadays, and what we plan to do.
The key for this feature to work is creating a global class that implements SandboxPostCopy and the method:

global void runApexClass(SandboxContext context)

Once you have that, you can do anything that you can do from Apex code.
In our case the trigger to use it was that we saw that after refreshing sandboxes from production, all the custom settings were brought as they are in production. This means that URLs that are pointing to an external productive web service were still there in sandboxes, with the risk of calling them (and generating wrong data).
In order to prevent that we implemented the following class:

global class SandboxManager implements SandboxPostCopy {

   global void runApexClass(SandboxContext context) {
      updateCurrentEnvironmentSetting();
   }

   @testVisible
   private void updateCurrentEnvironmentSetting(){
      CurrentEnvironment__c currentEnvironment =       CurrentEnvironment__c.getValues('currentEnvironment');
      currentEnvironment.environment__c = 'UAT';
      update currentEnvironment;
   }
}

As you can see the class implements SandboxPostCopy and it’s method runApexClass. In there we are calling a private method that updates the specific custom setting with a value different than the one in production. We separated it in a private method because later we would be adding more and more logic to update additional custom settings as well as creating some base test data that we want to have in every sandbox.

The test class for it looks like this:

@isTest
private class SandboxManagerTests {

    @testSetup
    static void createCommonData(){
        CurrentEnvironment__c currentEnvironment = new CurrentEnvironment__c();
        currentEnvironment.Name = 'currentEnvironment';
        currentEnvironment.Environment__c = 'Test';
        insert currentEnvironment;
    }

    @isTest
    static void testUpdateToCurrentEnvironment()
    {
        Id orgId = UserInfo.getOrganizationId();
        Id sandboxId = UserInfo.getOrganizationId();

        Test.startTest();
            Test.testSandboxPostCopyScript(new SandboxManager(), orgId, sandboxId, 'sandbox');
        Test.stopTest();

        CurrentEnvironment__c ce = CurrentEnvironment__c.getValues('currentEnvironment');

        System.assertEquals('UAT', ce.environment__c, 'Custom setting value should change');
    }
}

As you can see is a pretty simple test class. The key here is that if you want to test the runApexClass method you need to create those context variables. As the private method is with the @testVisible attribute, you can also create a test method specifically to test it (making it more as a unit test than just coverage).
That test method would look like:

@isTest
static void testUpdateToCurrentEnvironment()
{
    SandboxManager sandboxManager = new SandboxManager();

    Test.startTest();
        sandboxManager.updateTWGPortalURLSetting();
    Test.stopTest();

    CurrentEnvironment__c ce = CurrentEnvironment__c.getValues('currentEnvironment');

    System.assertEquals('UAT', ce.environment__c, 'Custom setting value should change');
}

As mentioned before, i have the idea to enhance this class to be able to create some common test data that we are seeing that is needed in every sandbox that we create, so that it facilitates the process and removes the excuse from the developers of not refreshing their sandbox so that they don’t loose the data they created 😉

Feel free to comment if you are using it and how you are using it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s