+ Start a Discussion
carmilyn.c martincarmilyn.c martin 

Running a Batch Class, error encountered.First error: Too many DML rows: 10001

Hello, 

I am having trouble running a batch class and I am encountering an error "First error: Too many DML rows: 10001". 

Here is the query and the batch class I am running on Dev Console: 

String query='Select Network_Name__c,Network_ID__c, Hub_Account__c from Account where Hub_Account__c !=NULL AND Network_Name__c !=NULL AND Network_ID__c !=NULL';
Id batchInstanceId = Database.executeBatch(new Batchable_Eisai_CrtTarget_Opn(query),200);

Need some help. Thank you.

Ming
Raj VakatiRaj Vakati
Hi Ming , Can you share your batch class "Batchable_Eisai_CrtTarget_Opn"  i will let you know what is the issue 

for time being you can reduce the batch size and try. 
Id batchInstanceId = Database.executeBatch(new Batchable_Eisai_CrtTarget_Opn(query),50);


 
carmilyn.c martincarmilyn.c martin
global class Batchable_Eisai_CrtTarget_Opn implements Database.Batchable<sObject>{

global final String Query;

   global Batchable_Eisai_CrtTarget_Opn(String q){
             Query=q;   
   }             

   global Database.QueryLocator start(Database.BatchableContext BC){
System.Debug('#######Start method called');
return Database.getQueryLocator(query);
   }

   global void execute(Database.BatchableContext BC,List<sObject> scope)
{
System.Debug('#######executeCalled');

System.Debug('#######AccountsList'+scope);

Eisai_CrtTarget_Opn_Listson_BackendLoad obj = new Eisai_CrtTarget_Opn_Listson_BackendLoad();
obj.function(scope);
 
}//end of execute
global void finish(Database.BatchableContext BC){

   }




}//end of batch class
carmilyn.c martincarmilyn.c martin
public Class Eisai_CrtTarget_Opn_Listson_BackendLoad
{
    private List<Account>Account_Recs = new List<Account>();
    private Map<Id,Set<Id>> TargetOpn_Account_Map = new Map<Id,Set<Id>>();
    private Set<String>Terriotries_Set = new Set<String>();
    private List<String>AccountIds = new List<String>();
    private List<Account_Territory_Loader_vod__c>Atl_Recs = new List<Account_Territory_Loader_vod__c>();
    private Set<String>Territories_Set = new Set<String>();
    private Map<Id,List<String>>Account_toTerritoryMap = new Map<Id,List<String>>();
    private List<UserRole>UserRole_List = new List<UserRole>();
    private List<ID>UserRoleIds_List = new List<ID>();
    private List<User>User_List = new List<User>();
    private Map<String,String>RoleNameToId_Map = new Map<String,String>();
    private Map<Id,Set<Id>>User_Account_AlignmentMap = new Map<Id,Set<Id>>();
    private Map<Id,Id>User_TerritoryMap = new Map<Id,Id>();
    private Map<Id,Set<Id>>TargetOpn_toUserMap=new Map<Id,Set<Id>>();
    private List<Account_List_vod__c>existngAlist = new List<Account_List_vod__c>();
    private Set<Id>UserIds_Set = new Set<Id>();
    private Map<String,Account_List_vod__c>existngAList_Map = new Map<String,Account_List_vod__c>();
    private Set<Id>TargetOpnIds = new Set<Id>();
    private List<Account>TargetOpnAccnts = new List<Account>();
    private Map<Id,String>TargetOpn_NamesMap = new Map<Id,String>();
    private Set<Account_List_vod__c>AlistToInsert = new Set<Account_List_vod__c>();
    private List<Account_List_vod__c>AlistToInsert_List = new List<Account_List_vod__c>();
    private Map<String,Account_List_vod__c>AlistToInsert_Map = new Map<String,Account_List_vod__c>();
    private Map<String,Account>updateAlist_Map = new Map<String,Account>(); 
    private List<Account>updateAlist = new List<Account>();
    private Map<String,Account_List_vod__c>presentAList_Map = new Map<String,Account_List_vod__c>();
    private List<Account_List_vod__c>presentAlist = new List<Account_List_vod__c>();
    private Set<Account_List_Item_vod__c>AlistItems_ToInsert = new Set<Account_List_Item_vod__c>();
    private List<Id>PreviousTargetOpnids_List = new List<Id>();
    private Map<Id,List<Id>>PreviousTargetOpn_ToAccountMap = new Map<Id,List<Id>>();
    private List<Account_List_Item_vod__c>previous_AlistItems = new List<Account_List_Item_vod__c>();
    private Map<String,Account_List_Item_vod__c>previous_AlistItems_Map = new Map<String,Account_List_Item_vod__c>();
    private Set<Account_List_Item_vod__c>DelAlistItems = new Set<Account_List_Item_vod__c>();
    private List<Account_List_vod__c>AccountList_cleanupList = new List<Account_List_vod__c>();
    private List<Account_List_Item_vod__c>DelAlistItems_list= new List<Account_List_Item_vod__c>();
    private List<Account_List_Item_vod__c>AlistItems_ToInsert_List = new List<Account_List_Item_vod__c>();
    
    public void function(List<Sobject>AccountObjList)
    { 
        { 
        Account_Recs =[Select Id,Hub_Account__c,Territory_vod__c,Network_Name__c,Network_ID__c 
from Account where Network_Name__c !=NULL AND Network_ID__c !=NULL AND Hub_Account__c !=NULL];
         for(Sobject sobj:AccountObjList)
         {
             Account obj=(Account)sobj;
             Account_Recs.add(obj); 
         }           
         for(Account a : Account_Recs)
         {
             if(a.Hub_Account__c!=null)
             {  
                 if(TargetOpn_Account_Map.containsKey(a.Hub_Account__c))
                 { 
                     Set<Id>temp_list=new Set<Id>();
                     temp_list= TargetOpn_Account_Map.get(a.Hub_Account__c);
                     temp_List.add(a.Id);
                     TargetOpn_Account_Map.put(a.Hub_Account__c,temp_List);
                 }//end of if
                 
                 else
                 {
                     Set<Id>temp =new Set<Id>();
                     temp.add(a.Id);
                     TargetOpn_Account_Map.put(a.Hub_Account__c,temp);
                 }//end of else
             } //end of outer if
             
             updateAlist_Map.put(a.ID,a);
             AccountIds.add(a.Id);
             if(a.Hub_Account__c!=null && a.Network_Name__c !=NULL && a.Network_ID__c !=NULL)
             { TargetOpnIds.add(a.Hub_Account__c); }
             
         }//end of for               
         
         Atl_recs =[SELECT Account_vod__c,Territory_vod__c 
                    FROM Account_Territory_Loader_vod__c WHERE Account_vod__c In:AccountIds  ];
         for (Account_Territory_Loader_vod__c atl:Atl_recs)
         {
             if(atl.Territory_vod__c!=null && atl.Territory_vod__c.length()>1)
             { 
                 String s1=atl.Territory_vod__c;
                 system.debug('s1---'+s1);
                 String s2=s1.substring(1,s1.length()-1);
                 List<String>Terr_List=new List<String>();
                 Terr_List=s2.split(';');
                 Account_toTerritoryMap.put(atl.Account_vod__c,Terr_List);
                 Territories_Set.addAll(Terr_List);
             }
         }
         UserRole_List=[SELECT Id,Name,ParentRoleId FROM UserRole WHERE Name In:Territories_Set];
         for(UserRole u:UserRole_List)
         { 
             if(string.valueof(u.name).startswith('ON') || string.valueof(u.name).startswith('MK'))
             {  
                 UserRoleIds_List.add(u.Id);
                 RoleNameToId_Map.put(u.Name,u.Id);
             }
         }
         
         User_List=[Select name,Id,UserRoleId , ProfileId
                    from User Where UserRoleId In:UserRoleIds_List and IsActive=true and (ProfileId = '00eA0000000yVZy' or ProfileId = '00e12000000fIav' or ProfileId = '00eA0000000yVS9')];
         for(User x:User_List)
         {
             User_TerritoryMap.put(x.UserRoleId,x.Id);
             UserIds_Set.add(x.Id);
         }
         for(Account a : Account_Recs)
         {
             List<String>AccTerrList = new List<String>();
             if(Account_toTerritoryMap.containsKey(a.Id))
                 AccTerrList=Account_toTerritoryMap.get(a.Id);
             for(String s:AccTerrList)
             { 
                 if(User_Account_AlignmentMap.containsKey(a.Id))
                 { 
                     Set<Id>tempUsr_list=new Set<Id>();
                     tempUsr_list= User_Account_AlignmentMap.get(a.Id);
                     tempUsr_List.add(User_TerritoryMap.get(RoleNameToId_Map.get(s)));
                     User_Account_AlignmentMap.put(a.Id,tempUsr_List);
                 }//end of if
                 
                 else
                 {
                     Set<Id>temp =new Set<Id>();
                     temp.add(User_TerritoryMap.get(RoleNameToId_Map.get(s)));
                     User_Account_AlignmentMap.put(a.Id,temp);
                 }//end of else
                 
             }//end of inner for
         }//end of outer for
         for(Id i :TargetOpn_Account_Map.KeySet())
         {
             Set<Id>aList = new Set<Id>();
             Set<Id>uList = new Set<Id>();
             Set<Id>uSet = new Set<Id>(); 
             aList=TargetOpn_Account_Map.get(i);
             
             for(Id b : aList)
             {
                 if(User_Account_AlignmentMap.containsKey(b)) 
                 { uList=User_Account_AlignmentMap.get(b);
                  uSet.addAll(uList);
                 }
             }
             TargetOpn_toUserMap.put(i,uSet);  
         }
         existngAList=[Select Id,name,OwnerId,CreatedById from Account_List_vod__c 
                       where Network_Name_and_Owner__c !=null and OwnerId IN:UserIds_Set];
         for(Account_List_vod__c e:existngAList)
         {
             existngAList_Map.put(e.name+'_'+e.OwnerId,e);
             
         }
         
         TargetOpnAccnts=[Select Id,name, Network_Name__c from Account where Id In:TargetOpnIds];
         for(Account z :TargetOpnAccnts)
         {
             TargetOpn_NamesMap.put(z.Id,z.Network_Name__c); //holds mapping from TargetOpnId to Name of TargetOpnAccount
         }
         
         for(Id Tid : TargetOpn_toUserMap.keySet() )
         {
             Set<Id>Accntusers = TargetOpn_toUserMap.get(Tid);
             for(Id i: Accntusers)
             {if(!existngAList_Map.containsKey(TargetOpn_NamesMap.get(Tid)+'_'+i))
             { Account_List_vod__c obj = new Account_List_vod__c();
              obj.Name = 'NTWK_' + TargetOpn_NamesMap.get(Tid);
              obj.ownerId = i;
              obj.createdById = i;
              if(obj.ownerId!=null)
              { AlistToInsert.add(obj);
               AlistToInsert_Map.put(obj.Name+'_'+obj.ownerId,obj);}
             }//end of if 
             }//end of inner for
         }
         
         if(AlistToInsert.size()>0)
         {AlistToInsert_List=AlistToInsert_Map.values();
          System.Debug('@@@@@size'+AlistToInsert.size()+'qqqqq'+AlistToInsert+'mmmmaap'+AlistToInsert_Map.size());
          insert AlistToInsert_List;
         }
         presentAList=[Select Id,name,OwnerId,CreatedById from Account_List_vod__c 
                       where Network_Name_and_Owner__c !=NULL and OwnerId IN:UserIds_Set];
         List<Id>AlistIds = new List<Id>();
         Map<String,Account_List_Item_vod__c>myexistingMap = new Map<String,Account_List_Item_vod__c>();
         for(Account_List_vod__c rec:presentAlist)
         {
             presentAList_Map.put(rec.name+'_'+rec.OwnerId,rec);
             AlistIds.add(rec.Id);
         }
         List<Account_List_Item_vod__c>myexistingList = new List<Account_List_Item_vod__c>();
         myexistingList=[Select Account_List_vod__c,Account_vod__c from Account_List_Item_vod__c 
                         where Account_List_vod__c In:AlistIds];
         for(Account_List_Item_vod__c a:myexistingList)
         {
             myexistingMap.put(a.Account_List_vod__c+'_'+a.Account_vod__c,a);
         }
         for(Id key : TargetOpn_Account_Map.keySet())
         {
             Set<Id>AccList = new Set<Id>();
             AccList=TargetOpn_Account_Map.get(key);
             for(Id a : AccList)
             {
                 Set<Id>UsersList = new Set<Id>();
                 if(User_Account_AlignmentMap.containsKey(a)) 
                 {
                     UsersList=User_Account_AlignmentMap.get(a);
                     for(Id u : UsersList)
                     {
                         Account_List_Item_vod__c obj = new Account_List_Item_vod__c();
                         if(presentAList_Map.containsKey('NTWK_' + TargetOpn_NamesMap.get(key)+'_'+u))
                         { 
                          if(!myexistingMap.containsKey(presentAList_Map.get('NTWK_' + TargetOpn_NamesMap.get(key)+'_'+u).Id+'_'+a))
                          {obj.Account_List_vod__c = presentAList_Map.get('NTWK_' + TargetOpn_NamesMap.get(key)+'_'+u).Id;
                           obj.Account_vod__c = a;
                           AlistItems_ToInsert.add(obj);
                          } 
                          //Create Account List Item for Target_Opn Account        
                          if(!myexistingMap.containsKey(presentAList_Map.get('NTWK_' + TargetOpn_NamesMap.get(key)+'_'+u).Id+'_'+key))
                          { Account_List_Item_vod__c tempObj = new Account_List_Item_vod__c();
                           tempObj.Account_List_vod__c = presentAList_Map.get('NTWK_' + TargetOpn_NamesMap.get(key)+'_'+u).Id;
                           tempObj.Account_vod__c = key;
                           AlistItems_ToInsert.add(tempObj);
                          }
                         } //end of if
                     }//end of 2nd inner for
                 }//end of if after t 1st inner for
             } //end of 1st inner 
         }//end of outer for
         AlistItems_ToInsert_List.addAll(AlistItems_ToInsert);
         if(AlistItems_ToInsert_List.size()>0)
         {insert AlistItems_ToInsert_List;}
        }//end of System.Label.Create_Target_Opn_List_BackendLoad=='True'.
    }//end of function
}//end of class
carmilyn.c martincarmilyn.c martin
I tried what you suggested: Id batchInstanceId = Database.executeBatch(new Batchable_Eisai_CrtTarget_Opn(query),50);
It gave me variable does not exist error which is "query" here is what I tried:

String query='Select Network_Name__c,Network_ID__c, Hub_Account__c from Account where Hub_Account__c !=NULL AND Network_Name__c !=NULL AND Network_ID__c !=NULL';
Id batchInstanceId = Database.executeBatch(new Batchable_Eisai_CrtTarget_Opn(query),50);

Gave me the same error message: First error: Too many DML rows: 10001.

Regards,
Ming