+ Start a Discussion
EldonEldon 

DML operation on setup object is not permitted after you have updated a non-setup object

Hi 

Im trying to create a community user in my test class having a contact associated with it. But when i insert the user im getting error.
 
account userAccount=new account(
           name='userAccount'
       );
       insert userAccount;
       
       contact userContact=new contact(
           lastname='userName',
           AccountId=userAccount.id
       );
       insert userContact;
       
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       system.debug('portalRole is ' + portalRole);
       
       
       Id p = [select id from profile where name='Partner Community User'].id;
       User user2 = new User(
           contactid=userContact.id,
           UserRoleId = portalRole.Id,
           ProfileId = p,
           Username = 'TestUserCheckOut@gmail.com',
           Alias = 'batman',
           Email='bruce.wayne@wayneenterprises.com',
           EmailEncodingKey='UTF-8',
           Firstname='Bruce',
           Lastname='Wayne',
           LanguageLocaleKey='en_US',
           LocaleSidKey='en_US',
           TimeZoneSidKey='America/Chicago'
       );
       insert user2;
       system.runAs(user2){
           
           LightningCartApexController.CheckOut();           
       }

Im getting teh following 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: []
 
Thank you
Best Answer chosen by Eldon
karthikeyan perumalkarthikeyan perumal
Hello Eldon, 

Use Below Code, 
 
account userAccount=new account(
           name='userAccount'
       );
       insert userAccount;
       
       contact userContact=new contact(
           lastname='userName',
           AccountId=userAccount.id
       );
       insert userContact;
	   User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
       system.runAs(thisUser){
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       system.debug('portalRole is ' + portalRole);      
      
       Id p = [select id from profile where name='Partner Community User'].id;
       User user2 = new User(
           contactid=userContact.id,
           UserRoleId = portalRole.Id,
           ProfileId = p,
           Username = 'TestUserCheckOut@gmail.com',
           Alias = 'batman',
           Email='bruce.wayne@wayneenterprises.com',
           EmailEncodingKey='UTF-8',
           Firstname='Bruce',
           Lastname='Wayne',
           LanguageLocaleKey='en_US',
           LocaleSidKey='en_US',
           TimeZoneSidKey='America/Chicago'
       );
           insert user2;                 
           LightningCartApexController.CheckOut();           
       }

Hope this will help you. Mark Best Answer if it works for you. 

Thanks
karthik

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello Eldon, 

Use Below Code, 
 
account userAccount=new account(
           name='userAccount'
       );
       insert userAccount;
       
       contact userContact=new contact(
           lastname='userName',
           AccountId=userAccount.id
       );
       insert userContact;
	   User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
       system.runAs(thisUser){
       UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
       system.debug('portalRole is ' + portalRole);      
      
       Id p = [select id from profile where name='Partner Community User'].id;
       User user2 = new User(
           contactid=userContact.id,
           UserRoleId = portalRole.Id,
           ProfileId = p,
           Username = 'TestUserCheckOut@gmail.com',
           Alias = 'batman',
           Email='bruce.wayne@wayneenterprises.com',
           EmailEncodingKey='UTF-8',
           Firstname='Bruce',
           Lastname='Wayne',
           LanguageLocaleKey='en_US',
           LocaleSidKey='en_US',
           TimeZoneSidKey='America/Chicago'
       );
           insert user2;                 
           LightningCartApexController.CheckOut();           
       }

Hope this will help you. Mark Best Answer if it works for you. 

Thanks
karthik

 
This was selected as the best answer
EldonEldon
Hi

Now im getting the following error,

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, role type must match user type: []
karthikeyan perumalkarthikeyan perumal
Hello Eldon, 

this line may causing the Error. 

UserRoleId = portalRole.Id--- Remove the line 

Then make sure the specified role is defined on the user running the test class insted of using ID try dummy role. 
 like below 

PortalRole ​='Manager'    -- Add this line insted of  UserRoleId 

it will resolvve the error.

Thanks
karthik