0

How can we execute SandboxPostCopy Interface from Anonymous Window for testing purpose.

global class PrepareMySandbox implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        System.debug('Org ID: ' + context.organizationId());
        System.debug('Sandbox ID: ' + context.sandboxId());
        System.debug('Sandbox Name: ' + context.sandboxName());

        // Insert logic here to prepare the sandbox for use.
    }
}
  • 2
    Typically, you'd write a Unit Test for testing purposes. – sfdcfox Dec 24 '17 at 1:16
3

It turns out SandboxContext is an interface you can implement. If you want to execute against a non-null context, you can do a mock implementation in your execute anonymous window:

public class MockSandboxContext implements SandboxContext
{
    public ID organizationID(){return '00D200000001111';}
    public ID sandboxID(){return '00D200000001112';}
    public ID sandboxName(){return '00D200000001113';}
}

public class MockSandboxCopy implements SandboxPostCopy
{
    public void runApexClass(SandboxContext context)
    {
        System.debug('Org ID: ' + context.organizationId());
        System.debug('Sandbox ID: ' + context.sandboxId());
        System.debug('Sandbox Name: ' + String.valueOf(context.sandboxName()));
        //For some reason the above line throws an UnexpectedException with the message
        //"Unexpected Arguments" if you do not use String.valueOf
    }
}

SandboxContext context = new MockSandboxContext();
new MockSandboxCopy().runApexClass(context);
  • This method isn't outlined in the documentation, and is thus subject to not work in the future (unless it's added to the documentation). That said, that's a pretty awesome hack. Hopefully they add it to the documentation and make it official. – sfdcfox Dec 24 '17 at 1:02
1

Change your code to:

global class PrepareMySandbox implements SandboxPostCopy {
    global void runApexClass(SandboxContext context) {
        if (context != null) {
          System.debug('Org ID: ' + context.organizationId());
          System.debug('Sandbox ID: ' + context.sandboxId());
          System.debug('Sandbox Name: ' + context.sandboxName());
        } 
        // Insert logic here to prepare the sandbox for use.
    }
}

and then execute anonymous

new PrepareMySandbox().runApexClass(null);

Type SandboxContext can't be constructed so your code, if you want to test in execute anonymous can't depend on a non-null context

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.