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?

  • This is on my to do list. Mar 1, 2016 at 8:55
  • @AtulRajguru9 Well, if you get it working, let me know if there are any tricks to getting it set up. I'll see if I can't figure it out myself, but it's hard to use brute force trial and error when each sandbox refresh takes so long.
    – martin
    Mar 1, 2016 at 11:44
  • 1
    @martin Have you tried using Test.testSandboxPostCopyScript to test it? I tried an account insertion with a SandboxPostCopy in a testing context, and it worked. The only other thing I can think of would be to intentionally throw an exception in the runApexClass method and then see where the smoke comes out. Mar 1, 2016 at 20:05
  • @DanielBallinger Good idea, my sandbox post refresh code previously had 0% test coverage. I created a test class and the test ran successfully in both the sandbox and in prod, and the debug logs showed that the account was inserted. However, after refreshing a sandbox, I still didn't see that account created.
    – martin
    Mar 2, 2016 at 3:13

2 Answers 2


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

  • Thanks for your post. It looks like they took a bit longer than they expected to get this up and running, but in my most recent sandbox refresh on April 7th, the script worked.
    – martin
    Apr 8, 2016 at 1:50
  • Has anyone gotten this to work now that the bug fix was released with Patch 16 this week? We are still having the same problem we were having before the patch was released. Run the code manually, and it works. Refresh the sandbox, and the code runs, no error, but no data. Debug log stops at the DML statement that inserts the accounts.
    – Lisa
    Apr 8, 2016 at 16:12

For my case my script didn't run successfully because of sending an email at the end, where in new sandboxes this option is disabled by default.

Just because of this, my previous DML was not commited, which is quite strange. I replace the send log in email with creating a log via documentcontent and was able to run the script on my new org.

I run the script in my org manually like this :

AP_PostSandboxRefreshActions ap = new AP_PostSandboxRefreshActions();

The code to write the log as a contentDocument is a follows :

ContentVersion cv = new ContentVersion();
cv.Title='Sandbox refresh log'; 
cv.PathOnClient ='log.txt';
cv.VersionData = blob.valueOf(log); //where log is a string 
cv.origin = 'H';
insert cv;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .