LoginSignup
0
0

MIXED_DML_OPERATION

Last updated at Posted at 2022-05-01

まとめページに戻る
まとめA~M

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): PermissionSetAssignment,

まぁ、たまに悩む。違うユーザで実行するのが定石かな。

User with custom permission on their permission set is enable to perform the update

コメントを読んでも何をしたかはよく分からん。

I was able to get rid of the Mixed DML error, below is my final code, user with the custom permission should be able to update the opportunity which is not working as of now in my case. The scenario works perfectly when I do it on the UI.
混合DMLエラーを取り除くことができました。以下は私の最終的なコードです。カスタム権限を持つユーザーは、私の場合、現在機能していない商談を更新できるはずです。 UIで実行すると、シナリオは完全に機能します。

Flow

As for the MIXED_DML_OPERATION error, this occurs when you try to perform DML operations on setup objects (such as User) and non-setup objects (such as Contact) in the same transaction. To avoid this error, you need to use asynchronous actions in your flow, such as scheduled actions or invocable actions.

Check Out: Automate This! — Resolve Mixed DML Errors with Platform Events - Salesforce Admins

As to the best flow. Keep it simple, use a Record Trigger Flow, and make sure you are selecting One for How many records you want to create.

I solved!! I changed the path to Asychoronusly

テストクラスでのエラー

久しぶりに古いApex classに機能を追加したのでテストを実行したら... 確かに数年前は問題なかったのに...

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

データ操作言語 (DML) 操作を実行するコードが System.runAs メソッドブロックで囲まれている場合は、テストメソッドで設定の sObject と設定以外の sObject を含む混合 DML 操作の実行が許可されます。また、テストメソッドがコールする非同期ジョブで DML も実行できます。たとえば、こうした方法を使用して、ロールのあるユーザーとその他の sObject を同じテスト内で作成できます。

あああ、そういえば何か仕様が変わったような。

ダメな例
見積情報登録がSystem.runAs(u){の外側にある

        // 事前準備
        User u = fkd_User.createTestUser();
        // 見積情報登録
        fkd_Property__c fkdPro01 = new fkd_Property__c(Name='CT_TEST2_01',
//        purchase_order_number__c='',
        tantoc__c=''
        );
        insert fkdPro01;

        // テスト実施
        Test.startTest();
            // 作成したユーザで処理を実行
            System.runAs(u){
                try {
                    // インスタンス化させたくないのであれば、コンストラクタはprivate宣言しておいた方が良いはず…
                    //Cls_EstimateCapture.csv(fkdPro01.Id, selectVal, capType);//旧
                    Cls_EstimateCapture fkdOMCls = new Cls_EstimateCapture();
                    fkd0002form.ResponseDto dto = Cls_EstimateCapture.csv(fkdPro01.Id, result, capType,item_type);//新
                    System.debug('★正常終了★');
                } catch(Exception e) {
                    System.debug('error ' + e.getMessage());
                }
            }
        Test.stopTest();

エラーが解消できた例
見積情報登録がSystem.runAs(u){の内側にある

        // 事前準備
        User u = fkd_User.createTestUser();
        // テスト実施
        Test.startTest();
        
            // 作成したユーザで処理を実行
            System.runAs(u){
                try {
                    // 見積情報登録
                    fkd_Property__c fkdPro01 = new fkd_Property__c(Name='CT_TEST2_01',
                                                                   //        purchase_order_number__c='',
                                                                   tantoc__c=''
                                                                  );
                    insert fkdPro01;
                    // インスタンス化させたくないのであれば、コンストラクタはprivate宣言しておいた方が良いはず…
                    //Cls_EstimateCapture.csv(fkdPro01.Id, selectVal, capType);//旧
                    Cls_EstimateCapture fkdOMCls = new Cls_EstimateCapture();
                    fkd0002form.ResponseDto dto = Cls_EstimateCapture.csv(fkdPro01.Id, result, capType,item_type);//新
                    System.debug('★正常終了★');
                } catch(Exception e) {
                    System.debug('error ' + e.getMessage());
                }
            }
        Test.stopTest();
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