0

I am writing a test class where the user with a specific custom permission on their permission set should be able to update the object records. This functionality totally works when doing the update through the UI, but not when running the test. Here is my code where I am creating a temp user, assigning the permission set and then manually adding and removing the custom permission to test both conditions.

This is error I get when running the test - 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): PermissionSetAssignment,

PermissionSet testPS = [SELECT PermissionSet.Id FROM PermissionSet WHERE Name = 'Admin'];

User testUser = TestingUtils.createUser('NonAlteryxUser', 'Admin');
insert new PermissionSetAssignment(AssigneeId=testUser.Id, PermissionSetId=testPS.Id);

System.runAs(testUser) {
    System.debug('CheckPermission before- '+FeatureManagement.checkPermission('CustomPermission'));
    testOpp.StageName = 'Identified;
        try {
            update testOpp;
        }
        catch(Exception ex) {
            message = ex.getMessage();
        }           
}

System.assert(message.contains('Error Message'));

SetupEntityAccess cp = new SetupEntityAccess(
    ParentId = testUser.Id,
    SetupEntityId = [SELECT Id FROM CustomPermission
    WHERE DeveloperName = 'CustomPermission'][0].Id
);
insert cp;


System.runAs(testUser) {
    System.debug('CheckPermission After- '+FeatureManagement.checkPermission('CustomPermission'));
    testOpp.StageName = 'Closed Won;
    update testOpp;                
}   
         
System.assertEquals('Closed Won', testOpp.StageName);

UPDATE AFTER SPLITTING THE METHOD

Here is my test method so far, but the assertion still fails which shouldn't happen. ALl the record creation is done in the same method.

User with this custom permission should be able to perform the update.

User adminUser = TestingUtils.buildUser('testUser', 'System Administrator');
        insert adminUser;

        User userWithCP = TestingUtils.buildUser('userWithCP', 'System Administrator');

        System.runAs(adminUser) {
            insert userWithCP;

            PermissionSet ps = new PermissionSet(Label = 'mockPs', Name = 'mockPs');
            insert ps;

            SetupEntityAccess sea = new SetupEntityAccess(
                ParentId = ps.Id,
                SetupEntityId = [SELECT Id FROM CustomPermission
                WHERE DeveloperName = 'Opportunity_Exempt_AER_OPP_032'][0].Id
            );
            insert sea;

            PermissionSetAssignment psa = new PermissionSetAssignment(
                AssigneeId = userWithCP.Id,
                PermissionSetId = ps.Id
            );
            insert psa;
        }      
        String msg = '';
        System.runAs(userWithCP) {
            System.debug('User with Custom Permission - '+FeatureManagement.checkPermission('Opportunity_Exempt_AER_OPP_032'));
            testOpp.Address__c = addresses[1].Id;
            try {
                update testOpp;
            }
            catch(Exception ex) {
                msg = ex.getMessage();
            }          
        }
        System.assert(!msg.contains('Error Message));

1 Answer 1

1

DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction.

In your test class you are creating an user and updating the opportunity in the same transaction because of which the error is occurring.

Below is the useful guide which has an example with the test class.

1
  • Hello SaiPravven, I went through the article you mentioned, I am still confused but still made a few changes based on what I understood of it. I have added an update to the original post to show you where I am at now. Could you please have a look and advice? Mar 8, 2022 at 17:09

You must log in to answer this question.

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