How to Resolve Mixed DML Operation Error

How to Resolve Mixed DML Operation Error

Have you ever experienced an error due to Mixed DML Operations?

A mixed DML operation occurs when we try to insert or update both setup and non-setup objects in a single transaction.

So, what are set-up and non-setup objects?

Non-Setup objects are standard objects like Accounts or any custom object.?Setup objects are Group1, GroupMember, QueueSObject, User, UserRole, UserTerritory, Territory, etc.

Recently, I had a task where I had to insert a GroupMember record along with a Contact record. I tried achieving this using Flow, but was getting the error below.

No alt text provided for this image
Mixed DML Operation Error

This error occurred because two different kinds of DML operations were used. This means that we cannot insert a GroupMember and a Contact record in a single transaction in the configuration, since the GroupMember is a setup object and Contact is a non-setup object.?

So, I realized this cannot be done using Salesforce Flow.

How to Fix Mixed DML Operation Error?

Synchronous and Asynchronous process:

"Asynchronous" means that a task is done in the background and doesn't stop or slow down other tasks from happening. This is useful for big or time-consuming tasks that don't need to be finished right away. An example of an asynchronous operation in Salesforce is a @future method.

?"Synchronous" means that a task is done at the same time as other tasks and blocks or stops other tasks from happening until it's finished. This is useful for tasks that need to be completed right away, such as creating a new record. An example of a synchronous task in Salesforce is creating a new account.

We can deal with this error by updating the GroupMember record asynchronously, using @Future method.

What is the `@future` method?

The @future method in Salesforce is a special type of code that allows you to perform tasks in the background without slowing down other important tasks in the system. It is used for tasks that take a longer time to complete and can be run asynchronously.

This means that the rest of the system can continue to work while the @future method is running. When the task is finished, the method returns the result.

Below is an example of how to use the `@future` method.

No alt text provided for this image
`@future` method. Example

Here the GroupMember was inserted Asynchronously using the `@future` method. This is how mixed DML operation errors can be solved using this method. Have any questions? Share it with us in the comments.

Author: Aruna James, Amdrodd Technologies Pvt. Ltd.

No alt text provided for this image

要查看或添加评论,请登录

Amdrodd Technologies LLC.的更多文章

社区洞察

其他会员也浏览了