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:
- 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
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the Flow Name that will be modified. In this case Auto Assign Permission Set Group.
- Click on the Save As button at the top right of the Flow builder.
- 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.
- On Flow Designer, click on the +icon and select the Create Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Input the following information:
- Enter Label the API Name will auto-populate.
- How Many Records to Create: One
- How to Set the Record Fields: Use separate resources, and literal values
- Object: Leave Balance
- Set Field Values for the Leave Balance
- Row 1:
- Field: Earned_Leave__c
- Value: 1
- Click Add Field
- Row 2:
- Field: Employee_Name__c
- Value: {!$Record.Id}
- Click Add Field
- Row 3:
- Field: Medical_Leave__c
- Value: 1
- Click Add Field
- Row 4:
- Field: Casual_Leave__c
- Value: 1
- 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
- Create a new User in Salesforce, as shown in the following screenshot:
- 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
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the Flow Name that will be modified. In this case Auto Assign Permission Set Group.
- Click on the Save As button at the top right of the Flow builder.
- 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.
- Under Start, select Add Scheduled Paths (Optional).
- Under SCHEDULED PATHS, click on the New Scheduled Path.
- Under Scheduled Path Details, enter the Label the API Name will auto-populate.
- Time Source: User: Created Date
- Offset Number: 0
- Offset Options: Hours After
- Advanced Options
- Batch Size: 200
- 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.
- On Flow Designer, below the 0 hr After node, click on the +icon and select the Get Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Select the Leave Balance object from the dropdown list.
- Select All Conditions Are Met (AND).
- Set Filter Conditions
- Row 1:
- Field: Employee_Name__c
- Operator: Equals
- Value: {!$Record.Id}
- Row 1:
- How Many Records to Store:
- select Only the first record
- How to Store Record Data:
- Choose the option to Automatically store all fields.
- 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.
- On Flow Designer, click on the +icon and select the Decision element.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!Get_Public_Group}
- Operator: Is Null
- Value: {!$GlobalConstant.False}
- Row 1:
- When to Execute Outcome: If the condition requirements are met.
- 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.
- On Flow Designer, click on the Leave Balance Not Found and select the Create Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Input the following information:
- Enter Label the API Name will auto-populate.
- How Many Records to Create: One
- How to Set the Record Fields: Use separate resources, and literal values
- Object: Leave Balance
- Set Field Values for the Leave Balance
- Row 1:
- Field: Earned_Leave__c
- Value: 1
- Click Add Field
- Row 2:
- Field: Employee_Name__c
- Value: {!$Record.Id}
- Click Add Field
- Row 3:
- Field: Medical_Leave__c
- Value: 1
- Click Add Field
- Row 4:
- Field: Casual_Leave__c
- Value: 1
- 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
- Create a new User in Salesforce, as shown in the following screenshot:
- 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.
- 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.
Hello Rakesh, Thanks for the reply but the “Pause” element is not there in the screen flows.
Hi, I am using the Screen flow to create contacts and make them community users in the system through the lightning flow. What option should I take?
Use the Pause element to separate the transaction.
Thanks and very helpful
Thanks, this was helpful to me! Saved me from the mixed DML error.
Thanks, this helped me solve an issue with a flow! very detailed!
Awesome!