Salesforce MIXED_DML_OPERATION error on PostInstall script in Managed Package

We’re having another strange bug in work today – we distribute our product as a Managed Package, and constantly have problems with this that only show up when we upload the package.

This week’s issue is a¬†MIXED_DML_OPERATION exception that we are getting, due to updating data in two objects, a custom setting and some PermissionSetAssignments.

We have been using the install scripts as a good way of getting around some restrictions in Salesforce (dml operations in constructors, etc.), and when shipping an updated package we need to update some data – easily done in a PostInstall script.

However, despite following the best practice of code separation using the @future annotation on functions, we are still getting a bug.

Here is the code I have built to set up the PermissionSetAssignments in the future context (which should be in a different context to the custom setting updates):

private static void insertPermissionSetAssignments(Map<Id, Id> psaAssignment){
  List permissionSetAssignments = new List();
    for(Id userId: psaAssignment.keySet()){
      PermissionSetAssignment psa = new PermissionSetAssignment(AssigneeId = userId, PermissionSetId = psaAssignment.get(userId));

As this is a PostInstall script we can’t use some of the usual hacks (system.runas) to avoid this error …



Well, this turned out to be a bit different to what I expected. It was a test class that was causing the error – the postinstall script was fine. So to fix it, we had to write code that would check if you were inside a test or not, and react accordingly…

  User adminuser = [select Id from User where Profile.Name = 'System Administrator' and IsActive = true limit 1];


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>