1

This trigger fires after inserting a User. It will create a new record on Leave_Balance_Transaction__c but Im encountering this error:

Review all error messages below to correct your data. Apex trigger autoCreateLBT4NewEmploy caused an unexpected exception, contact your administrator: autoCreateLBT4NewEmploy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Leave_Balance_Transaction__c, original object: User: []: Trigger.autoCreateLBT4NewEmploy: line 30, column 1

trigger autoCreateLBT4NewEmploy on User (after insert) {
    List <Id> ids = new List<Id>();
    Set<Id> Cids = new Set<Id>();
    List <Leave_Balance_Transaction__c> newLbT4newUser = new List<Leave_Balance_Transaction__c>();    
    List <Contact> tmpContact = new List<Contact>();
    List <User> tmpUser = new List<User>();

    for(User userID : Trigger.NEW) {
        ids.add(userID.id);
        Cids.add(userID.ContactId);    
    }    

    for(Contact con : [SELECT ID FROM Contact WHERE id IN : Cids AND no_of_years__c > 0]){
        if(con != null){
            tmpContact.add(con);
        }
    }

    for(User user1 : [SELECT ID, ContactId FROM User WHERE ContactId IN : tmpContact]){            
         newLbT4newUser.add(new Leave_Balance_Transaction__c( 
             employee__c = user1.id,
             active__c = 'Active'));      
             //
    }
    insert newLbT4newUser;        
}

thanks for your help in advance

3

Looks into this documentation.

For mix dml error use future method so it will solve the issue.

Mix dml error occurs when a non setup and setup object updated/inserted in same transaction then mix dml occurs.

We can avoid this error using future method.

Created a class with future method

public class createLeaveBalTransClass
{
    @future 
    public static void createLeaveBalTrans(set<Id> setContactids)
    {
        List <Leave_Balance_Transaction__c> newLbT4newUser = new List<Leave_Balance_Transaction__c>();    
        for(User user1 : [SELECT ID, ContactId FROM User WHERE ContactId IN : setContactids]){

              newLbT4newUser.add(new Leave_Balance_Transaction__c( 
              employee__c = user1.id,
              active__c = 'Active'));
        }
        insert newLbT4newUser;   
    }
}

now in trigger collect all contacts Id

createLeaveBalTransClass.createLeaveBalTrans(setContactids);
| improve this answer | |
  • hi ratan can you teach me how to summon @future class into trigger? If its okay – sfdclearner May 2 '16 at 6:49
  • @gelay I don't think you can do that because we can't create method in trigger it self that's why you need to create another class with future method and call that method from trigger and pass the set of contact Ids.. this way it will work.. – Ratan Paul May 2 '16 at 6:53
  • is there any way rather than using @future? – sfdclearner May 2 '16 at 8:24
  • @gelay I say no.. This is the only solution – Ratan Paul May 2 '16 at 8:42
  • how can i get the Trigger.new in the class? – sfdclearner May 2 '16 at 9:01
2

The issue is User is a setup object and you can not insert a non-setup object records after inserting the setup object records so you have to do this asynchronously.

See Setup objects in Salesforce

check this link:

https://developer.salesforce.com/forums/?id=906F00000008wAZIAY

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.