I finally got a sandbox refresh script written, tested, and deployed to prod yesterday. But when I actually a sandbox and have it run on the sandbox copy, I don't actually get the records it's supposed to create. Debug log shows this error: EXCEPTION_THROWN|[67]|System.TypeException: DML operation INSERT not allowed on Account

The beginning part of my script works fine, inserts three Chatter groups and a Task. But then when it starts the part that creates Accounts, Contacts, etc, it fails.

I found a reference to a known issue that says it was closed in Winter '16. (https://success.salesforce.com/issues_view?id=a1p300000008epFAAQ). No other references that seem relevant.

Anyone know what's up? (I'm not pasting in the code because it compiles, gets 100% test coverage, deploys to Prod, and can be executed in Execute Anonymous, so I think I'm safe in saying the only problem is with it running in sandbox refresh context.)

global class SandboxRefreshScript implements SandboxPostCopy {
global void runApexClass(SandboxContext context) {
    // Change value of Alias below to the Alias of user who should own the group
    // ensuring that it is unique among your users
    List<User> groupOwner = [SELECT Id, Username FROM User WHERE Alias = 'MKolo'];
    Id grpOwnerId = groupOwner[0].Id;

    // Create a Chatter Group for Payment Pipeline Changes
    List<CollaborationGroup> groups = new List<CollaborationGroup>();

    CollaborationGroup pipeline = new CollaborationGroup(
        CollaborationType = 'Public',
        Description = 'Group for showing changes to the cashflow pipeline. ',
        Name = 'Payment Pipeline Changes - sandbox',
        OwnerId = grpOwnerId

    //Create another Chatter Group for All Spark
    CollaborationGroup allSpark = new CollaborationGroup(
        CollaborationType = 'Public',
        Description = 'All Spark group. ',
        Name = 'All Spark - sandbox',
        OwnerId = grpOwnerId

    //Create another Chatter Group for Hiring Notifications
    CollaborationGroup hiringNotifications = new CollaborationGroup(
        CollaborationType = 'Private',
        Description = 'Hiring Notifications group. ',
        Name = 'Hiring Notifications - sandbox',
        OwnerId = grpOwnerId

    insert groups;

    //Create a Task for yesterday (so it's immediately overdue) that
    //reminds to update Process Builders to use the new Chatter Groups.
    Task newTask = new Task(
        Subject = 'Fix Process Builders',
        ActivityDate = Date.today()-1,
        OwnerId = grpOwnerId
    insert newTask;




  • 2
    A sample version of the code would still be useful. I'd be willing to ask my boss if we can deploy a harmless sample file, maybe get a repro, engage premier support, that sort of deal.
    – sfdcfox
    Jan 24 '19 at 22:33
  • Code coming, below. (You won't be able to use it directly because it's so dependent on our custom objects and record types.) Jan 25 '19 at 14:51
  • BTW- no need for you to engage Support. I have actually opened a case. But I wanted to also ask here in case someone knew something I am not finding.... Jan 25 '19 at 14:55
  • Awesome. Love the data factory class. I think this should be obvious enough that anyone who cares to try to repro this should be able to do so. Thanks for the edit.
    – sfdcfox
    Jan 25 '19 at 16:13
  • It's been several days since I opened a Premier Support case and I'm not too impressed. They wrote back that "The SandboxPostCopy interface runs under the context of the automated process user." and that I should edit that user's (or profile's) permissions. But that's not possible. Feb 4 '19 at 19:28

Your Answer

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

Browse other questions tagged or ask your own question.