Facade of the modern architecture

MIXED_DML_OPERATION Error: Setup and Non-Setup Objects

by Nikita Verkhoshintcev

I recently encountered an interesting error in the unit tests related to mixed DML operations that I'd like to share.

What happens if you try to insert Account and Permission Set Assignment objects in the same transaction?

You will receive a MIXED_DML_OPERATION error, indicating that you cannot insert both setup and non-setup objects simultaneously.

Note: You can update both setup and non-setup objects or insert non-setup and update setup objects in the same transaction.

It was precisely the error that an organization was facing when they asked me to check it.

In this post, I explain what these types of objects are, what one of the typical use cases is, and how to resolve them.

Setup and Non-setup objects

What are those setup and non-setup objects?

In short, setup objects are the metadata and configuration of your Salesforce organization instance that define how the organization is set up and functions.

For example, these include Profiles and Permission Sets that control user access and permissions, user objects, Custom Settings, Custom Metadata Types, and more.

The non-setup, on the other hand, refers to the set of standard and custom objects used to store and manage business data, such as Accounts, Contacts, and Opportunities.

The Problem

Usually, you don't encounter that error.

However, the organization had a particular case. They had a process in place to automate the assignment of permission sets to new Experience Cloud users via a record-triggered flow on their creation.

A straightforward flow that creates a user and inserts a Permission Set Assignment record.

Additionally, there were unit tests that also aimed to test user permissions.

The unit tests had test setup methods to generate test data.

Therefore, because they created both the business data and users in the same test setup, it triggered the Flow automation, and the new permission set assignment caused the mixed DML operation error.

The Solution

Technically, this error is an expected behaviour, and what you need to do is to ensure you use different transactions to insert the setup and non-setup objects.

If you use Apex methods, consider offloading them via async methods (@future or queueable).

For the flow, the most straightforward approach is to create the scheduled path with a 0-minute delay.

Nikita Verkhoshintcev photo

Nikita Verkhoshintcev

Senior Salesforce Technical Architect & Developer

I'm a senior Salesforce technical architect and developer, specializing in Experience Cloud, managed packages, and custom implementations with AWS and Heroku. I have extensive front-end engineering experience and have worked as an independent contractor since 2016. My goal is to build highly interactive, efficient, and reliable systems within the Salesforce platform. Typically, companies contact me when a complex implementation is required. I'm always open to collaboration, so please don't hesitate to reach out!

Let's work together!

Do you have a challenge or goal you'd like to discuss? We offer a free strategy call. No strings attached, just a way to get to know each other.

Book a free strategy call

Stay updated

Subscribe to our newsletter to get Salesforce tips to your inbox.

No spam, we promise!