Getting Started with Process Builder – Part 64 (How to fix MIXED_DML_OPERATION error)

Getting Started with Process Builder – Part 64 (How to fix MIXED_DML_OPERATION error)

Last Updated on April 15, 2019 by Rakesh Gupta

Process Builder is a way of automating business processes. It is an upgraded version of the Workflow Rule. Whenever a user creates a process, the system automatically creates a Visual Workflow – and a Flow Trigger (which is now deprecated) – to call the Flow. Creation of the Flow happens behind the scenes; and therefore, a user does not need to interact with these ‘shadow’ (behind the scenes) Flows. Process Builder 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 process for each requirement – or creating processes without understanding Salesforce limitations – then, a user may encounter many issues, either in future or, while testing/deployment phase. Let us start with one of the most common error   Mixed Data Manipulation Language (DML) Operation Error.

What is 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 Process Builder to avoid getting a Mixed DML Operation Error.

First, I will perform DML operation on Setup (User) and non-Setup (a Custom Object called Leave Balance) 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 Time-dependent/Scheduled Action, in the Process Builder, to perform DML operations on Setup and non-Setup Objects in the same Process 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 Universal Containers. 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 update Allow Forecasting checkbox on the user record.
  2. Auto Credit one leave for each category – Medical, Casual and Earned.
Custom object - Leave balance
Custom object – Leave balance

Let us create a Process that generate a MIXED_DML_OPERATION error

There are few possible solutions for the above business scenario, but we will use the Process Builder to solve it. Follow the  instructions below to create a Process for the above business requirement 

1.  Use Lightning Experience, click on Setup | PLATFORM TOOLS | Process Automation | Process Builder
2.
 To create a new process from scratch, click on the New Button available on Process Management page. A pop-up will appear where you have to enter the Name (Use Auto credit leaves to new hire as name)API Name and Description as shown in the below screenshot

Define Process Properties
Define Process Properties

3. Click on Object node to add object and then select User object. For the entry criteria, Select only when a record is created, as shown in the screenshot below. Once done, click on the Save button

Evaluation Criteria
Evaluation Criteria

4. The next task is to add Process Criteria. To do this click on Add Criteria, then enter NameType of action and set filter conditions as shown in the following screenshot.

  •  [User].IsActive Equals Boolean True
  •  [User].ForecastEnabled Equals Boolean False
Process Criteria
Process Criteria

5. The next step is to add an Immediate action to the Process, to update Allow Forecasting to True and auto credit leaves balances. First of all, add an immediate action to update Allow Forecasting checkbox to true, as shown in the following screenshot 

Update Records (Immediate action) - Grant forecasting permission to user
Update Records (Immediate action) – Grant forecasting permission to user

Now, add one more immediate action to credit leaves balances to new hire, as shown in the following screenshot 

Create a Record (Immediate action) - Credit leaves
Create a Record (Immediate action) – Credit leaves

6. Once done, click on the Save button, it will redirect you to Process canvas. Finally, the Process will look like the following screenshot

Don’t forget to activate the Process by clicking on the Activate button.

It’s time to test the Process

Create a new User in Salesforce, as shown in the following screenshot

New user setup
New user setup

Once you’re done, click on the Save button. Boooommmm, you encounter a MIXED_DML_OPERATION error, as shown in the following screenshot 

MIXED_DML_OPERATION error
MIXED_DML_OPERATION error

–> As mentioned earlier, the reason behind the error is, we are updating user record and creating leave balances in the same transaction. It means we are performing DML operations on setup and non-setup object in the same transaction. It means we are performing DML operations on setup (User) and non-setup (Leave Balance) object in the same transaction.

Fix MIXED_DML_OPERATION error

To fix the MIXED_DML_OPERATION error in a Process Builder or a Flow, we have to separate setup and non-setup objects transactions by using time-dependent/scheduled action. Perform the following instructions to solve the MIXED_DML_OPERATION error in a Process

1. Click on Setup | PLATFORM TOOLS | Process Automation | Process Builder
2.
 Open Auto credit leaves to new hire Process, click on the Clone button available on the button bar and create a Version of current process

Clone a Process
Clone a Process

–> Once a process is activated, the user cannot make any changes to it. If a user wants to make changes to a Process, then, a user has to clone a Process and save it, either as a New Version or, as a New Process.

3. Now we have to delete immediate action Credit leaves. 
4. The next step is to add a Scheduled action to the Process. Under Scheduled Actions, click Set Schedule. Set the schedule for 0 Hours after the account CreatedDate

Set Schedule
Set Schedule

5. Now that you have a schedule added to your process, next step is to add the Immediate Action action to Credit leaves, as shown in the following screenshot 

Create a Record (Scheduled action) - Credit leaves
Create a Record (Scheduled action) – Credit leaves

6. Once done, click on the Save button, it will redirect you to the Process canvas. Finally, the Process will look like the following screenshot

Don’t forget to activate the Process by clicking on the Activate button.

It’s time to test the Process

Create a new User in Salesforce, as shown in the following screenshot

New user setup
New user setup

Once you are done, click on the Save button. It will redirect you to a user’s record detail page. Notice that, Allow Forecasting checkbox is updated by the Process.

Allow Forecasting
Allow Forecasting

Wait a few minutes and then navigate to Leave Balances tab. A new leave record is created for new hire Ian Young, as shown in the following screenshot 

Leave Balance - Record
Leave Balance – Record

 

Technical Editor and Proofreader: - Munira Majmundar
If you have feedback, suggestions for posts or need more information about Salesforce online training program running by me. Say hello and leave and message to me!

6 thoughts on “Getting Started with Process Builder – Part 64 (How to fix MIXED_DML_OPERATION error)

Leave a Reply

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