SandboxPostCopy Interface
Namespace
Usage
Create an Apex class that implements this interface. Specify your class during sandbox creation. After your sandbox is created, the runApexClass(context) method in your class runs using the automated process user’s permissions.
SandboxPostCopy Methods
The following method is for SandboxPostCopy.
runApexClass(context)
Signature
public void runApexClass(System.SandboxContext context)
Parameters
- context
- Type: System.SandboxContext
- The org ID, sandbox ID, and sandbox name for your sandbox. To work with these values, reference context.organizationId(), context.sandboxId(), and context.sandboxName() in your code.
Return Value
Type: void
SandboxPostCopy Example Implementation
This example implements the System.SandboxPostCopy interface.
global class PrepareMySandbox implements SandboxPostCopy {
global PrepareMySandbox() {
// Implementations of SandboxPostCopy must have a no-arg constructor.
// This constructor is used during the sandbox copy process.
// You can also implement constructors with arguments, but be aware that
// they won’t be used by the sandbox copy process (unless as part of the
// no-arg constructor).
this(some_args);
}
global PrepareMySandbox(String some_args) {
// Logic for constructor.
}
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.
}
}
The following example tests the implementation using the System.Test.testSandboxPostCopyScript() method. This method takes four parameters: a reference to a class that implements the SandboxPostCopy interface, and the three fields on the context object that you pass to the runApexClass(context) method. An overload on the method takes an optional Boolean parameter to indicate if the test must be performed as the Automated Process user.
@isTest
class PrepareMySandboxTest {
@isTest
static void testMySandboxPrep() {
// Insert logic here to create records of the objects that the class you’re testing
// manipulates.
Test.startTest();
// Replace '00D000000000000' with your sandboxId and
// execute test script with RunAsAutoProcUser set to true.
Test.testSandboxPostCopyScript(
new PrepareMySandbox(), UserInfo.getOrganizationId(),
'00D000000000000', UserInfo.getOrganizationName(), true);
Test.stopTest();
// Insert assert statements here to check that the records you created above have
// the values you expect.
}
}
For more information on testing, see Testing Apex.