+ Start a Discussion
prani rachaprani racha 

Too many DML rows: 10001

 
Hi , I have written  apex class and trigger for territory management piece where whenever account is created based on territory ID then it should query custom settings table and should assign temebers roles to the team members that are having the same territory id and also the email address should map the mail address in salesforce users list.
Class, trigger, test class are working fine and I am getting the error:
               Sandbox

Apex script unhandled exception by user/organization: 005q0000000Qlme/00Dq00000009Orr
Source organization: 00DU0000000LYdk (null)
Failed to process batch for class 'rh2.PS_Rollup_Batch' for job id '707q0000002UIGr'

caused by: System.LimitException: Too many DML rows: 10001

Class.AccountTeamMember_Class.accountTeamAssignUpdate: line 125, column 1
Trigger.AddAccountTeamMember: line 6, column 1
 
Whenever batch class runs every one hour am getting this error
Please anyone help me out how to make my trigger bulk friendly and suggest me how to resolve this issue.

Apex class and trigger:


public class AccountTeamMember_Class{
    public static void accountTeamAssignInsert(List<Account> newList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;
             
        for (Account acc : newList) {
            uid = acc.ownerId;
            for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where email in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                    for(User u : usr){
                    system.debug('Acc.ownerId:::'+Acc.ownerId);
                        AccountTeamMember atm = new AccountTeamMember();
                        AccountShare  nas = new AccountShare();
                        atm.AccountId = acc.id;                
                        atm.TeamMemberRole = s.TeamMemberRole__c;
                        atm.UserId= u.id; 
                        latm.add(atm);
                        
                       if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){
                         
                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);
        
                        }
                               
                    }    
                }
            }
            
            try{
            
                insert(las);
                insert(latm);
             
                       
            
            }catch(DMLException e){
        
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
    //=============================================UPDATE====================================================================
    public static void accountTeamAssignUpdate(List<Account> newList,List<Account> oldList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;
        String oldterritory_id;
        String newterritory_id;
        for(Account a : oldList){
            oldterritory_id = a.Territory_ID__c;
        }
        for(Account a : newList){
            newterritory_id = a.Territory_ID__c;
        }
        if(oldterritory_id != newterritory_id){
            for (Account acc : newList) {
                setids.add(acc.id);
            }
            
            List<AccountTeamMember> oldatm = [select id from AccountTeamMember where AccountId in : setids];
            if(oldatm != null){
                delete(oldatm);
            }
        }
        for (Account acc : newList) {
            uid = acc.ownerId;
            for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where email in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                    for(User u : usr){
                    system.debug('Acc.ownerId:::'+Acc.ownerId);
                        AccountTeamMember atm = new AccountTeamMember();
                        AccountShare  nas = new AccountShare();
                        atm.AccountId = acc.id;                
                        atm.TeamMemberRole = s.TeamMemberRole__c;
                        atm.UserId= u.id; 
                        latm.add(atm);
                        
                       if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){
                         
                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);
        
                        }
                               
                    }    
                }
            }
            
            try{
            
                insert(las);
                insert(latm);
             
                       
            
            }catch(DMLException e){
        
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
}

Trigger:

trigger AddAccountTeamMember on Account (after insert,after update) {
      if(Trigger.isInsert){
        AccountTeamMember_Class.accountTeamAssignInsert(trigger.new);
    }
    if(Trigger.isUpdate){
        AccountTeamMember_Class.accountTeamAssignUpdate(trigger.new,trigger.old);
    }
}
Best Answer chosen by James Loghry
James LoghryJames Loghry
As your error indicates, you are only allowed to insert, update, delete, etc 10,000 rows per transaction.  One way to break these records up into smaller, more managable transactions is to createa a batchable class that handles the deletes in batches of up to 2000 records at a time.  For instance, see the following document and the "deleting records" example: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm