Mixed DML
Development
Kumaresan  

[FIX] Mixed DML Error In Salesforce

What is Mixed DML Error?

In our salesforce environment, Sometimes we can’t perform DML  operation between setup sObjects and non-setup sObjects.  In this kind of scenarios we will have an error that is Mixed DML error.

Setup Object Example

  • User
  • Profile
  • Etc..

Non-Setup Object Example

  • Lead
  • Account
  • Contact
  • Etc..

Solution To Fix Mixed DML Error In Salesforce

Note: To analyse the code copy it and paste it in notepad for the convenience.

public class TriggerUtility {

/*

1. Following future method execute asynchronously (whenever server is free it will execute in future context).

2. We should not declare @future method in Apex Trigger.

3. @future method should be always static.

4. @future method accepts only primitive data types (Integer, String, Boolean, Date, etc…) as parameters and it won’t accept

non-primitive data types (sObject,Custom Objects and standard Objects etc.. ) as parameters.

5. @future method should not contain return type. Always it should be void.

6. From an apex trigger we can make only make asynchronous call outs. To make call out we should include “callout = true” beside the future @annotation.

7. We cannot perform synchronous call outs from Apex Trigger.

*/

//Below is the example for the future method –

@future(callout = true)

public static void processAsync(primitive parameters) {

//Logic to insert/update the standard/custom object.

}

}

Leave A Comment