As we know Salesforce does not allow DML on Setup Objects and Non-Setup Objects in single transaction. Last week I was working on a requirement that requires updating setup objects (User and Permission Set) and non-setup objects contacts. I did not want to use APEX so asked my team member to use Salesforce Lightning Flow.
After few hours my team member came to me and explained the mixed DML issue that Salesforce flow is throwing. This was not the first time I hear about mixed DML but when I suggested flow-based solution, I completely missed to think about that. Well, I thought this would be good idea to put my learning in this article.
Business Requirement
When a new contact record of specific record type is created with status value as “Active” or an existing contact record with same record type is updated to set status as “Active”, we have to do few things: –
- Update few fields on associated asset record
- Create a new task and assign to Account Manager (a user field on associated account)
- Send an email to Account Manager
- Assign few permission sets to associated experience cloud user record
We can see it requires DML on both setup related objects and non-setup related objects.
We had other variation of this requirement also for scenario where contact status is updated from “Active” to “Inactive”
Initial Solution
Create a Salesforce lightning record triggered flow that runs on contact object and set of decisions and steps to update asset record, create task, send email, add permission set to user etc…
Problem with initial solution
As you see we are updating setup and non-setup objects in single transaction, we got mixed DML exception. We also found that we are doing lot of things in single transaction, it was not only throwing exception but at the same time it was taking 3-4 seconds to complete the flow and we started suspecting that it may take even more time once we deploy this in environment with millions of records.
Also, some of the operations depends on number of assets, so a contact with 10+ assets would be taking long time to complete the transaction.
Revised Solution
As you should be knowing Salesforce record triggered flow you can have a main path and can add one or multiple asynchronous / scheduled path. This you can co-related with future method we use in APEX.
I added a path in Salesforce flow for DML related to setup related object. This fixed the mixed DML issue and kind of met our requirement. We decided to go with this approach but then thought of making our solution more scalable and efficient. Well, nothing wrong in this approach and if you are facing mixed DML in flow then you can explore this option. Your flow would be looking something like below, but you can add more paths to your flow as needed.
Solution that we implemented
Well, this added few more hours and changes to our existing flow, but we believed this is going to be better fit for our requirement and would be more flexible and scalable for future need. Summary of our solution is explained here: –
- Created a simple Platform event with two fields
- Contact Id: Id of contact record
- Action: It will be populated with two values
- Add: If a new contact record is created with status as Active
- DEL: If an existing contact record is updated with status as Inactive
- Created a record triggered flow that runs on contact object and publish a platform event with action as “ADD” or “DEL” based on status value
- Created multiple platform event triggered Salesforce flows that triggers when a platform event is published and performs the actions as needed. Since these flows run in background and smaller size, they were very fast, reusable and easy to maintain.
- This solution was scalable as we can add more flows if we have to perform additional actions in future.
Well, I am sure we can have more solutions to address mixed DML issue in flow. I just wanted to share a use case with potential solutions. As we know learning is a journey and not the destination and I am on this journey with you so please validate and use your due diligence before going with above solutions