如何插入可用于测试的用户?

问题描述 投票:0回答:1

我需要编写一些顶点测试代码,我想在其中模拟一个用户,然后通过测试必须找到该用户。

我在创建一个时似乎遇到了一些问题?

到目前为止,我了解我需要一个帐户、联系人和联系人引用的用户。

但是当我尝试插入用户时似乎遇到了问题:

这是到目前为止的代码

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
}

我收到的错误消息是

MIXED_DML_OPERATION,更新非设置对象后不允许对设置对象进行 DML 操作(反之亦然):用户,原始对象:帐户:[]

我不明白,用户是一个设置对象,但它仅在帐户和联系之后创建 - 那么问题是什么,为什么我不允许这样做?

salesforce apex apex-code
1个回答
0
投票

要解决此问题,您需要将代码拆分为单独的事务。您可以使用 'System.runAs' 方法在单独的上下文中执行用户创建部分。 通过使用 System.runAs,您可以使用运行测试的用户的上下文来执行用户创建部分,从而允许您在安装对象上执行 DML 操作,而不会违反 MIXED_DML_OPERATION 限制。 `

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

`

© www.soinside.com 2019 - 2024. All rights reserved.