3

I keep getting following exception when trying to assign permission set to a user in test class:

System.DmlException: Insert failed. First exception on row 0; 
first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after 
you have updated a non-setup object (or vice versa): TriggerSettings__c, 
original object: PermissionSetAssignment: []

(TriggerSettings__c is a hierarchy custom setting that is generated in the test class) From the error message above (and this document) it appears that you can not create PermissionSetAssignment records together with any other sObjects in the same transaction. Does anyone have any thoughts on how to work around this in test cases?

[UPD1] Interestingly enough, same test case executed from MavensMate Test Runner UI does not produce above exception (originally, I was running tests from Developer Console). Any thoughts on why this might be happening are welcome as well.

Below is sample test class:

@isTest
private class AccountTriggerHandlerTest {

    @isTest static void testChangeLog() {
        TestUtil.initTest();
        ...
    }
}

And TestUtil class that generates custom setting and user and assigns it a permission set:

public with sharing class TestUtil {

public static User salesmen;

public static void initTest() {

    salesmen = getSalesmen();
    initTriggerCS();
}

public static User getSalesmen() {

    if (TestUtil.salesmen != null) {
        return TestUtil.salesmen;
    }

    Profile sales = [SELECT Id FROM Profile WHERE Name = 'Sales'];

    User u = new User();
    u.Username = 'sales@test.com';
    u.LastName = 'sales';
    u.Email = 'sales@test.com';
    u.Alias = 'sales';
    u.CommunityNickname = 'sales';
    u.TimeZoneSidKey = 'GMT';
    u.LocaleSidKey = 'en_GB';
    u.EmailEncodingKey = 'UTF-8';
    u.ProfileId = sales.Id;
    u.LanguageLocaleKey = 'en_US';
    insert u;

    PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'Provenance_Description_Ownership'];
    PermissionSetAssignment psa = new PermissionSetAssignment();
    psa.AssigneeId = u.Id;
    psa.PermissionSetId = ps.Id;
    insert psa;

    return u;
}


public static void initTriggerCS() {
    TriggerSettings__c settings = TriggerSettings__c.getOrgDefaults();
    settings = new TriggerSettings__c();

    DescribeSObjectResult describe = TriggerSettings__c.sObjectType.getDescribe();
    for (Schema.sObjectField f : describe.fields.getMap().values()) {
        Schema.DescribeFieldResult res = f.getDescribe();
        if (res.isCustom()) {
            if (res.getType() == Schema.DisplayType.Boolean) {
                settings.put(res.getName(), true);    
            } else {
                settings.put(res.getName(), res.getDefaultValue());
            }
        }
    }

    insert settings;
        insert settings;
}
10

We get this error when we are trying to perform DML on setup and non-setup objects in the same transaction.

Try to use System.runAs() in your test class code

Try to use this code:

System.runAs(new User(Id = Userinfo.getUserId())) {
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'Provenance_Description_Ownership'];
    PermissionSetAssignment psa = new PermissionSetAssignment();
    psa.AssigneeId = u.Id;
    psa.PermissionSetId = ps.Id;
    insert psa;
}
0

Use System.runAs() to have separate transacation for Setup and non-setup objects.

I would suggest you to enclose the insert custom setting record OR permission set record with system.runAs(User){} block.

-1

I'll admit I did not put enough effort to verify other answers, however I was able to solve my issue by simply not inserting the PermissionSetAssignment record in my test case.

Now my code looks like this:

PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'Provenance_Description_Ownership'];
PermissionSetAssignment psa = new PermissionSetAssignment();
psa.AssigneeId = u.Id;
psa.PermissionSetId = ps.Id;
//insert psa; - Notice commented out insert statement

return u;

This granted test user necessary permissions without causing MIXED_DML_EXCEPTION error.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.