Activity Forums Salesforce® Discussions What is MIXED-DML-OPERATION error and how to avoid?

  • Anurag

    Member
    September 10, 2018 at 2:42 pm

    Hi Prachi,

    Mixed DML operations within a single transaction aren’t allowed. You can’t perform DML on a setup sObject and another sObject in the same transaction. However, you can perform one type of DML as part of an asynchronous job and the others in other asynchronous jobs or in the original transaction. This class contains an @future method to be called by the class in the subsequent example.

    Use @future to Bypass the Mixed DML Error in a Test Method

    public class InsertFutureUser {
    @future
    public static void insertUser() {
    Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
    UserRole r = [SELECT Id FROM UserRole WHERE Name='COO'];
    User futureUser = new User(firstname = 'Future', lastname = 'User',
    alias = 'future', defaultgroupnotificationfrequency = 'N',
    digestfrequency = 'N', email = '[email protected]',
    emailencodingkey = 'UTF-8', languagelocalekey='en_US',
    localesidkey='en_US', profileid = p.Id,
    timezonesidkey = 'America/Los_Angeles',
    username = '[email protected]',
    userpermissionsmarketinguser = false,
    userpermissionsofflineuser = false, userroleid = r.Id);
    insert(futureUser);
    }
    }

  • Parul

    Member
    September 10, 2018 at 6:17 pm

    Hi

    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.

     

    Thanks

  • Avnish Yadav

    Member
    September 11, 2018 at 7:20 am

    Hello,

    Performing DML operation on more than two or two standard/custom object in the same transaction this error will come.

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

    Hope this will help you.

    Thanks.

  • shariq

    Member
    September 11, 2018 at 2:09 pm

    Hi,

    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.

    Thanks

Log In to reply.

Popular Salesforce Blogs