+ Start a Discussion
sfdc@isha.ax1814sfdc@isha.ax1814 

System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Class.CustomTeamMemberTriggerBL.DeleteInactiveMembers: line 26, column 1

Hi Everyone,

I ha ve below code. Iam trying to delete the customteammember based on the inactive users(User is a lookup field on customteammember object).  Iam getting below error.


System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: Class.CustomTeamMemberTriggerBL.DeleteInactiveMembers: line 26, column 1


Code:

public class CustomTeamMemberTriggerBL{

 public static void DeleteInactiveMembers(Map<id,CustomTeamMember__c> newRecords){
 
    set<Id> cmIDs = new set<Id>();
    for(CustomTeamMember__c cm:newRecords.values()){
       cmIDs.add(cm.Distribution_List__c);
    }
    
    List<CustomTeams__c> cus = new List<CustomTeams__c>();
    List<CustomTeamMember__c> cus1 = new List<CustomTeamMember__c>();
      
    cus = [SELECT Id,Name FROM CustomTeams__c WHERE id IN: cmIDs];
    
      for(CustomTeams__c ct: cus){
     
        for(CustomTeamMember__c cm: newRecords.values()){
        
                
            if( ct.id == cm.Distribution_List__c && cm.User__r.IsActive == false){
                cus1.add(cm);
            }                   
        }        
     }  
      
    Delete cus1;
     
   }
   
  }

Please help me on this code and any changes please let me know.

Regards,
Isha
 
Best Answer chosen by sfdc@isha.ax1814
Maharajan CMaharajan C
Hi Isha,

You trying to make the dml in trgger.new Records in after insert...which is not allowed in after transaction....

So in the CustomTeamMemberTriggerBL helper class you are passing the trigger.new Records right so by using that you can Query the record again from the databases from CustomTeamMember__c object by like this
List<CustomTeamMember__c>  newlist = Select Id from CustomTeamMember__c where ID IN: newRecords.keyset(); // add the remaining fields in query...

Then use this list instead of newRecords.Values in CustomTeamMemberTriggerBL

This will solves you issue..

All Answers

abhishek singh 497abhishek singh 497
Hello Isha,
Are you using before event in your trigger??

Thanks & Regards,
Abhishek Singh.
sfdc@isha.ax1814sfdc@isha.ax1814
Hi Abhishek,

Iam using in after insert in trigger event.

My Handler class is below.


public with sharing class CustomTeamMemberTriggerHandler{

    public static void onAfterInsert(Map<Id, CustomTeamMember__c> newRecords)
    {    
        CustomTeamMemberTriggerBL.DeleteInactiveMembers(newRecords);
    }

}


This iam calling on after insert in triiger
sfdc@isha.ax1814sfdc@isha.ax1814
 Hi Abhishek,

Iam calling on after insert in trigger.Below is my handler class.



public with sharing class CustomTeamMemberTriggerHandler{

    public static void onAfterInsert(Map<Id, CustomTeamMember__c> newRecords)
    {    
        CustomTeamMemberTriggerBL.DeleteInactiveMembers(newRecords);
    }

}
abhishek singh 497abhishek singh 497
Hello Isha,
Please use 'database.delete' instead of simple delete.
I hope it will solve your issue.
Please let me know if it works or not.

Thanks.
sfdc@isha.ax1814sfdc@isha.ax1814
Hi Abhishek,

​​​​​​Iam using DML Like below.

database.Delete(cus1);


But still iam getting the same issue.

 
abhishek singh 497abhishek singh 497
Hello Isha,
from where you are passing (Map<Id, CustomTeamMember__c> newRecords) as parameter in public static void onAfterInsert(Map<Id, CustomTeamMember__c> newRecords) 
the newRecords instance is trigger.old or trigger.new ???
sfdc@isha.ax1814sfdc@isha.ax1814
HI,

i hope you understand the code. my trigger code is below.


trigger CustomTeamMemberTrigger on CustomTeamMember__c (after insert) {
  if (UtilityTrigger.isTriggerEnabled('CustomTeamMemberTrigger '))
  {
    if (Trigger.isAfter)
    {
      // insert event
      if (Trigger.isInsert)
      {
        CustomTeamMemberTriggerHandler.onAfterInsert(Trigger.newMap);
      } 
    }    
  }
}

Handler class:

public with sharing class CustomTeamMemberTriggerHandler{

    public static void onAfterInsert(Map<Id, CustomTeamMember__c> newRecords)
    {    
        CustomTeamMemberTriggerBL.DeleteInactiveMembers(newRecords);
    }

}

code:

public class CustomTeamMemberTriggerBL{

 public static void DeleteInactiveMembers(Map<id,CustomTeamMember__c> newRecords){
 
    set<Id> cmIDs = new set<Id>();
    for(CustomTeamMember__c cm:newRecords.values()){
       cmIDs.add(cm.Distribution_List__c);
    }
    
    List<CustomTeams__c> cus = new List<CustomTeams__c>();
    List<CustomTeamMember__c> cus1 = new List<CustomTeamMember__c>();
      
    cus = [SELECT Id,Name FROM CustomTeams__c WHERE id IN: cmIDs];
    
      for(CustomTeams__c ct: cus){
     
        for(CustomTeamMember__c cm: newRecords.values()){
        
                
            if( ct.id == cm.Distribution_List__c && cm.User__r.IsActive == false){
                cus1.add(cm);
            }                   
        }        
     }  
      
    Delete cus1;
     
   }
   
  }
Maharajan CMaharajan C
Hi Isha,

You trying to make the dml in trgger.new Records in after insert...which is not allowed in after transaction....

So in the CustomTeamMemberTriggerBL helper class you are passing the trigger.new Records right so by using that you can Query the record again from the databases from CustomTeamMember__c object by like this
List<CustomTeamMember__c>  newlist = Select Id from CustomTeamMember__c where ID IN: newRecords.keyset(); // add the remaining fields in query...

Then use this list instead of newRecords.Values in CustomTeamMemberTriggerBL

This will solves you issue..
This was selected as the best answer
abhishek singh 497abhishek singh 497
Hello Isha,
You can't directly perform dml on trigger instance Like in your case it's trigger.new
You can take those same records and perform DML on them if you simply change the reference.
List<Event> nonTriggerEvents = new List<Event>();
for (Event triggerEvent : trigger.new)
{ nonTriggerEvents.add(new Event(Id = triggerEvent.Id)); }
update nonTriggerEvents;

I hope it will fix your issues.
sfdc@isha.ax1814sfdc@isha.ax1814
Hi Abhishek,Maharajan,


Thanks for the help.