You need to sign in to do that
Don't have an account?
Eldon
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.
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
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
Use Below Code,
Hope this will help you. Mark Best Answer if it works for you.
Thanks
karthik
All Answers
Use Below Code,
Hope this will help you. Mark Best Answer if it works for you.
Thanks
karthik
Now im getting the following error,
Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, role type must match user type: []
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