function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jude Kristian bautistaJude Kristian bautista 

Please help me guys :( urgent

this is my test class and i'm not familiar in this error :

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): 
User, original object: Account: []


My Test Class:
@isTest(seeAllData=true)
private class GHI_Portal_Login_Test {

    private static testMethod void testLogin() {
        RecordType rtContact = [SELECT Id FROM RecordType WHERE SObjectType='Contact' AND Name='Patient' LIMIT 1];
        Account acct = OSM_DataFactory.createAccount('test test');
        insert acct;
        Contact ctct = OSM_DataFactory.createContact('test','test',rtContact.Id);
        ctct.AccountId = acct.Id;
        insert ctct;
        
        
        User testUser = new User();
        testUser.Username= 'testUser11@company.com';
        testUser.Email = 'testuser1@company.com';
        testUser.Lastname = 'user';
        testUser.Firstname = 'test';
        testUser.Alias = 'test';
        testUser.CommunityNickname = '12346';
        List<Profile> prof = [select Id from Profile where Name = 'System Administrator' LIMIT 1];
        List<UserRole> role =  [select Id from UserRole where Name = 'finance'];
        testUser.UserRoleId = role[0].Id;
        testUser.ProfileId = prof[0].Id;
        insert testUser;
        
        User user = new User();
        user.Username= 'testUser1@company.com';
        user.Email = 'testuser1@company.com';
        user.Lastname = 'user';
        user.Firstname = 'test';
        user.Alias = 'test';
        user.CommunityNickname = '12346';
        List<Profile> prof1 = [select Id from Profile where Name = 'GHI Portal User' LIMIT 1];
        user.ProfileId = prof1[0].Id;
        user.ContactId = ctct.Id;
        
        System.debug ( JSON.serializePretty( testUser ) );
        
        
        
        PageReference pageRef = Page.GHI_Portal_Login;
        GHI_Portal_Login controller = new GHI_Portal_Login();
        Test.setCurrentPage(pageRef);
        
        Test.startTest();
        System.runAs(testUser) {
            insert user;
        }
        controller.username = 'testUser1@company.com';
        controller.password = '123456';
        controller.login();
        Test.stopTest();
    }

}
Zuinglio Lopes Ribeiro JúniorZuinglio Lopes Ribeiro Júnior

Hello pal,

That error is caused due to the fact that some sObjects cannot be mixed on the same transaction. Try something like this:

User mixedDmlOperationUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
System.runAs(mixedDmlOperationUser) {

       // Your tests goes here!

}

Find more about: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects_test_methods.htm

Hope have helped! Regards

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.