How to Fix MIXED DML Operation Error

How to Fix MIXED DML Operation Error

Last Updated on April 18, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

  • How do you resolve MIXED_DML_OPERATION: DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa) error? 

The Record-Triggered Flow is a way of automating business processes. Record-Triggered Flow is a powerful tool for system administrators and developers to implement business processes without writing code. However, with great power comes great responsibility –  If a user starts creating a new flow for each requirement – or creating flows without understanding Salesforce limitations – then, a user may encounter many issues, either in the future or, while the testing/deployment phase. Let us start with one of the most common errors   Mixed Data Manipulation Language (DML) Operation Error.

Objectives:

After reading this blog, you’ll be able to: 

  • Understand what is mixed DML operation error
  • Resolve mixed DML operation error
  • and much more

What is a Mixed DML Operation Error?

When DML operations on certain sObjects sometimes referred to as setup objects (such as a User Object), along with, on the non-setup object (such as Accounts), in the same transaction, then, a Mixed DML error is thrown at a User.

The Mixed DML error is generated – when a User performs DML actions on Setup and non-Setup Objects in the same transaction -because some sObjects affect the user’s access to records in the org. Therefore, a user must insert or update these types of sObjects in different transactions to prevent operations from happening with incorrect access-level permissions. For example, a user cannot update an account, and a user role, in a single transaction. However, deleting a DML operation has no restrictions (reference Salesforce help).

Below, I will show you how to use Salesforce Flow to avoid getting a Mixed DML Operation Error.

First, I will perform DML operations on Setup (User) and non-Setup (CollaborationGroupMember) Objects in the same transaction.  This will generate a Mixed DML Operation error because one cannot perform a DML operation on Setup and non-Setup objects in the same transaction.

Next, I will show you how to use a Time-dependent or Scheduled Path, in the Salesforce Flow, to perform DML operations on Setup and non-Setup Objects in the same Flow without incurring a Mixed DML operation error. The time-dependent/scheduled action is used because it helps in separating DML operations on Setup and non-Setup objects.

Business Use case

Donna Serdula is working as a System Administrator at Gurukul on Cloud (GoC). They just implemented Leave Management System in Salesforce. Serdula has a requirement from her manager that, as soon as a new user is created in Salesforce do the following:

  1. Auto Credit one leave for each category – Medical, Casual and Earned.

Pre-requisites

Please go through this article first and make sure to complete the listed steps Profile? So Yesterday! – Auto Assign Permission Set Group to a User

Instead of creating a new flow, we will use the existing after-save flow on the User object.

Create a Flow that Generates a MIXED_DML_OPERATION Error

To solve this requirement, we will modify the flow created for Profile? So Yesterday! – Auto Assign Permission Set Group to a User. Let’s begin building this automation process.

Step 1: Clone Existing Flow 

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the Flow Name that will be modified. In this case Auto Assign Permission Set Group.
  4. Click on the Save As button at the top right of the Flow builder.
  5. Click Save.

Step 2: Add Action – Create a Record

 The next step is to create a record to add leave balances record for new users, we will use the Create Records element.

  1. On Flow Designer, click on the +icon and select the Create Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: One
    3. How to Set the Record Fields: Use separate resources, and literal values
    4. Object: Leave Balance
    5. Set Field Values for the Leave Balance
    6. Row 1:
      1. Field: Earned_Leave__c
      2. Value: 1
    7. Click Add Field
    8. Row 2:
      1. Field: Employee_Name__c
      2. Value: {!$Record.Id}
    9. Click Add Field
    10. Row 3:
      1. Field: Medical_Leave__c
      2. Value: 1
    11. Click Add Field
    12. Row 4:
      1. Field: Casual_Leave__c
      2. Value: 1
  4. Click Done.

In the end, Donna’s Flow will look like the following screenshot:

Almost there! Once everything looks good, click the Activate button.  

Proof of Concept

  1. Create a new User in Salesforce, as shown in the following screenshot:
  2. Once you’re done, click on the Save button. Boooommmm, you encounter a MIXED_DML_OPERATION error, as shown in the following screenshot:

👉 As mentioned earlier, the reason behind the error is, that we are assigning Permission Set Group record and creating Leave Balances in the same transaction. It means we are performing DML operations on setup and non-setup objects in the same transaction.

Fix MIXED_DML_OPERATION Error

To fix the MIXED_DML_OPERATION error in a Flow, we have to separate setup and non-setup objects transactions by using

  • Scheduled Path 
  • Async Path
  • Pause element 
  • Platform Event
  • Scheduled-Triggered Flow
  • Future callout via Apex

Perform the following instructions to solve the MIXED_DML_OPERATION error in a Flow using Scheduled Path. The best way to solve this problem is using Platform Event, given that we want to enforce one after save flow per object. 

Step 1: Clone Existing Flow 

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the Flow Name that will be modified. In this case Auto Assign Permission Set Group.
  4. Click on the Save As button at the top right of the Flow builder.
  5. Click Save.

Step 2: Salesforce Flow – Add Scheduled Paths

The purpose of using a scheduled path is to execute the flow after 0 hours of user creation. The purpose of putting time-dependent action is to avoid MIXED DML errors.

  1. Under Start, select Add Scheduled Paths (Optional).
  2. Under SCHEDULED PATHS, click on the  New Scheduled Path.
  3. Under Scheduled Path Details, enter the Label the API Name will auto-populate.
  4. Time Source: User: Created Date
  5. Offset Number: 0
  6. Offset Options: Hours After
  7. Advanced Options
    1. Batch Size: 200
  8. Click Done.

Step 3: Adding a Get Record Element to Find the Leave Balance Record for User 

The next step is to use the Get Records element to find the leave balance record for the current record. 

  1. On Flow Designer, below the 0 hr After node, click on the +icon and select the Get Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Leave Balance object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: Employee_Name__c
      2. Operator: Equals
      3. Value: {!$Record.Id}
  6. How Many Records to Store:
    1. select Only the first record
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 4: Using Decision Element to Check If Leave Balance Record was Found or Not 

Now, will use the Decision element to check if the previous Get Records element returns a leave balance record or not. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Get_Public_Group}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  5. When to Execute Outcome: If the condition requirements are met.
  6. Click Done.

Step 5: Add Action – Create a Record

 The next step is to create a record to add leave balances record for new user, we will use the Create Records element.

  1. On Flow Designer, click on the Leave Balance Not Found and select the Create Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: One
    3. How to Set the Record Fields: Use separate resources, and literal values
    4. Object: Leave Balance
    5. Set Field Values for the Leave Balance
    6. Row 1:
      1. Field: Earned_Leave__c
      2. Value: 1
    7. Click Add Field
    8. Row 2:
      1. Field: Employee_Name__c
      2. Value: {!$Record.Id}
    9. Click Add Field
    10. Row 3:
      1. Field: Medical_Leave__c
      2. Value: 1
    11. Click Add Field
    12. Row 4:
      1. Field: Casual_Leave__c
      2. Value: 1
  4. Click Done.

In the end, Donna’s Flow will look like the following screenshot:

Almost there! Once everything looks good, click the Activate button.  

Proof of Concept

  1. Create a new User in Salesforce, as shown in the following screenshot:
  2. Once you are done, click on the Save button. It will redirect you to a user’s record detail page. Notice that, Permission set group is auto-assigned by the record-triggered flow.
  3. Wait a few minutes and then navigate to the Leave Balances tab. A new leave record is created for new hire Ian Young, as shown in the following screenshot:

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Preferred Timing(required)

13 thoughts on “How to Fix MIXED DML Operation Error

  1. Hi Rakesh,

    I am trying to create a record-triggered flow on User object which will execute in case the username is changed. This will be primarily used for community users. In the flow, I have a send email action to inform the users regarding change of username.

    The issue that I am facing is that I am not able to log this send email activity in either the related Contact or Account object. I tried using ‘Log email on send’ option but it is giving an error. Also, tried to manually create an activity record using Email Message and Email Message Relation but even that is not working.

    I am thinking that is this issue happening because of the mixed DML operation. As I am trying to update setup object record – User and create non-setup object record – Contact/Account. I tried to use schedule path(asynchronous) option in the flow, but that path never executes.

    Please let me know if there is a way to solve this issue.

    Thanks

  2. This is great. I am facing this issue in a flow that is doing the following:

    1. When a user record is changed from IsActive = True to IsActive = False
    2. Get all Opportunity records owned by that user, loop through them, and in an assignment variable do one of these two things (decision node) along with adding the record to a collection:
    -If there is a manager listed on the user’s record, assign the opportunity to the manager
    -If there is not a manager listed on the user’s record, assign the opportunity to a specific user in the org
    3. Update Opportunity records with record collection from the loop
    4. Find any Opportunity Split records owned by the deactivated user and delete them

    This process is resulting in the Mixed DML Operation Error, and I don’t understand why because there are no updates being made to any user records in the flow, just updating Opportunity Ownership and deleting Opportunity Split records. Where do you suggest I look for the issue here? Thank you!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading