LoginSignup
0
0

More than 1 year has passed since last update.

Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, 非設定オブジェクトを更新した後の設定オブジェクト上の DML 操作 (またはその逆) は、許可されていません: Account、元のオブジェクト: User: []

Last updated at Posted at 2022-10-11

何でだろうなぁ。今まで機能していたのに急に動かなくなった感じです。

static testMethod void  Modal_mainTest() {
        User u = fkd_User.createTestUser();
        
        sobject[] recordTypeList = [select Id, Name from RecordType WHERE Name = '得意先' ];   
        Account myAcc = new Account(Name = 'xxxxxx',
                                    customer_code__c='14xyz',
                                    RecordTypeId = recordTypeList[0].Id
                                   );
        insert myAcc;
        
        Test.startTest();
        // 作成したユーザで処理を実行
        System.runAs(u){
            
			UpdateData.update_Account();            
        }
        Test.stopTest();
    }

System.runAs(u){の位置を変えるとエラーが消えた

static testMethod void  Modal_mainTest() {
        User u = fkd_User.createTestUser();
        // 作成したユーザで処理を実行
        System.runAs(u){
        
        sobject[] recordTypeList = [select Id, Name from RecordType WHERE Name = '得意先' ];   
        Account myAcc = new Account(Name = 'xxxxxx',
                                    customer_code__c='14xyz',
                                    RecordTypeId = recordTypeList[0].Id
                                   );
        insert myAcc;
        
        Test.startTest();
			UpdateData.update_Account(); 
        Test.stopTest();
        }    
    
}

テストメソッドでの混合 DML 操作

しかし、本番環境にはこの書き方のテストクラスがいっぱいあるけど、デプロイしてもエラーにならなかったです。
もしかしてsandboxだけの問題かな?

単にこの書き方が悪るいのかもしれない。
ユーザを作るプロセスと他のレコード作成プロセスをTest.startTest()あるいはSystem.runAs(u){で分けたら問題ない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0