Run Script After Sandbox Creation and Refresh

With the Spring ’16 release, there is new functionality where we can set a script to run after refreshing a sandbox. In the documentation it gives an example where it prints the three context variables to a debug log.

Not being sure where that debug log would be stored or how to retrieve it, I created a similar class (api v36.0) that I applied to a developer sandbox (Spring ’16, as is prod.):

global class Sandbox_Refresh implements SandboxPostCopy {
  global void runApexClass(SandboxContext context) {
      insert new Account(name = 'Org Id: ' + context.organizationId() + ' Sandbox Id: ' + context.sandboxId() + ' Sandbox Name: ' + context.sandboxName());

After creating and also later when refreshing the sandbox, there was no account created, even though running very similar code as anonymous Apex right afterwards worked just fine:

insert new Account(name='test');

I also tried adding a test class, which ran fine in both the sandbox and production. Even when the sandbox post copy code had 100% test coverage, I still wasn’t able to see the account created in the sandbox after a refresh.

public class Test_Sandbox_Refresh{
    public static @isTest void runTest(){
        Sandbox_Refresh ClassInstance = new Sandbox_Refresh();
        Test.testSandboxPostCopyScript(ClassInstance, UserInfo.getOrganizationId(), '00Dp00000000hjM', 'sandbox2');

Now I’m not sure why the Sandbox Refresh script didn’t work, or where to find a log to trouble-shoot the issue. Has anyone gotten a Sandbox Refresh script to run successfully, and if so, do you have any tips for trouble-shooting when it doesn’t work properly?


I had the same issue and raised a ticket for Salesforce support team.

They came back saying there is a bug at their end and they are working on fixing it. Fix is expected to be available by end of next week (safe harbor).

Here is the response I got from Salesforce Support team.

Hi Arpit,

Hope you are doing good.

I would like to inform you that our R&D team has identified this issue
as a bug and are working on it to fix the same. I’ll keep you posted
of further updates on this one.

Ayush Sodhi
Success Agent- Tier 2

Source : Link , Question Author : martin , Answer Author : Mark Pond

Leave a Comment