0

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?

1
  • The error says you cannot update a setup object after updating a non-setup object, which is exactly what you're doing. What if you reverse the order? Does it work? Commented Jan 30, 2024 at 21:21

1 Answer 1

0

To resolve this issue, you need to split your code into separate transactions. You can use the 'System.runAs' method to perform the user creation part in a separate context. By using System.runAs, you're executing the user creation part with the context of the user who is running the test, allowing you to perform DML operations on setup objects without violating the MIXED_DML_OPERATION restriction. `

public static void testUserDataCreation() {
             // Insert Account and Contact
            Account acc = new Account(
                Name = 'Test Account' + '1',
                BillingStreet = 'Somestrasse 25',
                BillingCity = 'Edinburgh',
                BillingPostalCode = '123442',
                BillingCountry = 'Germany'
            );
            insert acc;
    
            Contact c = new Contact(LastName = 'Test', Email = '[email protected]', AccountId = acc.Id);
            insert c;
        
            // Perform User creation in a separate context
            System.runAs(new User(Id = UserInfo.getUserId())) {
                Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
                Userrole roleidval = [SELECT Id, Name
                                        FROM UserRole
                                        WHERE Name='CEO'];
                // 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 = roleidval.Id
                );
    
                // Insert the user
                insert u;
            }
        
              }

`

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.