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 

Guys can you help me in my problem ?

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();
    }

}
 
logontokartiklogontokartik
I am not sure if you are trying to create testUser just to make it work, you can directly use the below right?

You can wrap the complete code in System.runAs and it should take care of MIXED_DML_ISSUE

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_non_mix_sobjects_test_methods.htm
 
My Test Class:
@isTest(seeAllData=true)
private class GHI_Portal_Login_Test {

    private static testMethod void testLogin() {
        
     User currentUser = [Select Id, Name from User where Id = :UserInfo.getUserId()];
     System.runAs (currentUser) {
          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();
         insert user;
         controller.username = 'testUser1@company.com';
         controller.password = '123456';
         controller.login();
        Test.stopTest();
    }

 }

}