I need writing some apex test code, where I want to mock a user which I then via the test has to find.
I seem to have some problems creating one though?
So far I understand I need an account, Contact and a User which the contact references to.
But I seem to run into problems when I try to insert the user:
Here is the code so far
public static Auth.UserData createUserDataWithProfile() {
Account acc = new Account(
name = 'Test Account' + '1',
BillingStreet = 'Somestrasse 25',
BillingCity = 'Edinburg',
BillingPostalCode = '123442',
BillingCountry = 'Germany'
);
insert acc;
Contact c = new Contact(LastName = 'Test', Email = '[email protected]', AccountId = acc.Id);
// Insert the contact
insert c;
Profile p = [SELECT Id FROM Profile WHERE Name = 'customname'];
// Create a new user
User u = new User(
Alias = 'standt',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Testing',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
UserName = '[email protected]',
UserRoleId = '00EVe000000PD26MAG'
);
// Insert the user
insert u; // here it fails
}
The error message I get is
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: []
Which I don't get, User is a setup object, but its only created after account and contact - so what is the problem, why am I not allowed to this?