10

The new Sandbox Post Copy tasks are a great addition and I'm eager to try them but until the release to Production starts on Feb. 6, it seems impossible to experiment with the behavior. I reconstructed the sample in a sandbox and while I can get the unit test to pass, I can't figure out a way to invoke the code so that it returns the sandbox's ID and Name. The sample code requires passing the values to the class the implements SandboxPostCopy but my interpretation of the feature is that when it's invoked post-copy, those values in SandboxContext will be derived from the new sandbox itself.

My intention is to run a Lead/Contact email address sanitizer that will imbed the real email address in an email address that we control (e.g. ourinbox+someaddress.somebiz.com.sboxname@gmail.com) so we can safely enable Email Deliverability and monitor all outbound emails as well as tell both the contact email and sandbox from which they came, so getting the sandboxName value is necessary.

2
  • i was about the post the same question. I was wondering if the class that actually does the work could be called by other means (e.g. execute anonymous) for testing/development and all the SandboxPostCopy does is provide the trigger event for real usage. – Girbot Jan 30 '16 at 14:39
  • According to developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… you could use Test.testSandboxPostCopyScript(apexClassName, 'orgID', 'sandboxID', 'sandboxName'); to call a SandboxPostCopy class, but I can't seem to get it to work in my org (and I made sure to make the testclass in api 36). – Folkert Feb 22 '16 at 14:25
4

couldn't find much apart from this http://www.terrasky.co.jp/blog/2016/160218_001578.php

My Kanji is a bit rusty, so thanks to google translate it's helped me build test class to cover a class I've created that implements SandboxPostCopy interface to then execute a batch process to mass create test data records with specific relationships when sandbox is created/refreshed.

it looks like the test method for this is in the format testSandboxPostCopyScript(instantiated class variable name,orgId,sandboxId,'sandbox name')

@isTest
private class SandboxCreateBulkTestDataTest {

    @isTest static void testSandboxPostCopyScript() 
    {
        SandboxCreateBulkTestData cls = new SandboxCreateBulkTestData();
        Test.testSandboxPostCopyScript(cls,                  // the instantiated class that implements the SandboxPostCopy interface
                                       UserInfo.getOrganizationId(), // context org ID
                                       UserInfo.getOrganizationId(), // context SandboxID
                                       'sandbox');
        System.assertEquals(1,1,'XYZ test has failed');
    }
}

Hope this helps get going :-)

4

Salesforce Idea: Allow SandboxPostCopy to access @isTest Classes

Salesforce Spring ’16 Release Notes: Run Script After Sandbox Creation and Refresh says you have the following methods available from SandboxContext

  • organizationId()
  • sandboxId()
  • sandboxName()

So you could make your email

String email ='ourinbox' +
    '+' + context.organizationId() + 
    '+' + context.sandboxId() + 
    '+' + context.sandboxName() + 
    '@gmail.com';

Example:

global class HelloWorld implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        System.debug(
            'Hello Tester Pester ' + 
            context.organizationId() + ' ' + 
            context.sandboxId() + ' - ' + 
            context.sandboxName()
        );
    }
}

Code Example to see SandboxContext

global class SandboxDataProvider implements SandboxPostCopy {

    global SandboxDataProvider() {

    }

    global class TestException extends Exception {
        /**
         * Version 1.0.0:
         *      Created.  
         */
    }

    global void runApexClass(SandboxContext context){
        /**
         * Version 1.0.0:
         *      Created.  
         */
        throw new TestException(
            '\nSandboxContext: ' + context + '\n' +
            '\ncontext.organizationId: ' + context.organizationId() + '\n' +
            '\ncontext.sandboxId: ' + context.sandboxId() + '\n' +
            '\ncontext.sandboxName: ' + context.sandboxName() + '\n'
        );
    }

}

@isTest
public class SandboxDataProvider_test {

    @isTest
    public static void testPostCopy(){
        /**
         * Version 1.0.0:
         *      Created.  
         */
        Account org = new Account(
            Name = 'org'
        );

        Account sandbox = new Account(
            Name = 'sandbox'
        );

        insert new Account[]{
            org, 
            sandbox
        };

        // -------------------------------------------  Start Test  -------------------------------------------
        Test.startTest();

        // Using any Ids as orgId and sandboxId for test, e.g. Account Ids 
        Test.testSandboxPostCopyScript(
            new SandboxDataProvider(), 
            org.Id, 
            sandbox.Id, 
            'MySandboxName'
        );      

        Test.stopTest();
        // -------------------------------------------  Stop Test  -------------------------------------------

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.