Activity Forums Salesforce® Discussions How to handle mixed DML Exception in salesforce?

  • PRANAV

    Member
    April 26, 2018 at 4:43 pm

    How to handle mixed DML Exception in salesforce?

  • Manpreet

    Member
    April 26, 2018 at 4:50 pm

    Hi pranav,

    Whenever you are getting this error it means that two Sobjects(setup & non-setup) that your using in your code can not mix during the same transactions.To avoid this use the the System.runAs block or the @ future method.

    Thanks.

  • Parul

    Member
    September 21, 2018 at 4:17 am

    If we perform DML operation on standard/custom object and global objects(User, UserRole, Group, GroupMember, Permission Set, etc…) in same transaction this error will come.

    To avoid this error, we should perform DML operation on standard/custom object records in a different transaction.

    In general all the apex classes and apex triggers execute synchronously (execute immediately).

    If we perform DML operation on standard/custom object records asynchronously (execute in future context), we can avoid MIXED-DML-OPERATION error.

    To execute logic asynchronously keep the logic in an apex method (in a separate apex class, not in same apex trigger) which is decorated with @future annotation.

    See the below example:

    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.

    }

    }

     

    Thanks

  • Parul

    Member
    September 28, 2018 at 8:43 pm

    DML operations on certain sObjects, sometimes referred to as setup objects, can’t be mixed with DML on other sObjects in the same transaction. This restriction exists because some sObjects affect the user’s access to records in the org. You must insert or update these types of sObjects in a different transaction to prevent operations from happening with incorrect access-level permissions. For example, you can’t update an account and a user role in a single transaction. However, deleting a DML operation has no restrictions.

  • sumit saini

    Member
    August 2, 2021 at 12:30 pm

    Hi Pranav,
    You can get only dml exception from parent exception.
    Please follow the below link for reference.
    https://sfdctechsolutions.blogspot.com/2021/07/show-only-dml-exception-message-from.html

Viewing 1 - 5 of 5 posts

Log In to reply.

Popular Salesforce Blogs