+ Start a Discussion
learnSFlearnSF 

System.Exception: Too many DML rows: 244: Trigger.FindMarketTerritoryFromMarket: line 80, column 5

trigger FindMarketTerritoryFromMarket on Territory_Market__c (after insert, after update) { String dealerProspect = 'Dealer - Prospect'; //dealer prospect recType1 String dealerCustomer = 'Dealer - Customer'; // dealer cusomter recType2 String formerCustomer = 'Dealer - Former Customer'; Map<Id,Territory_Market__c> territoryMarket= new Map<Id,Territory_Market__c>(); for(Territory_Market__c t : Trigger.new) { if (System.Trigger.isInsert){ if(t.Parent_Market__c!=null && t.Territory_County__c!=null && t.Territory_State__c!=null) { territoryMarket.put(t.Id,t); system.debug('new parent name'); } }else if(t.Parent_Market__c!=Trigger.oldMap.get(t.Id).Parent_Market__c){ territoryMarket.put(t.Id,t); system.debug('new parent name updated'); } } /***********************************/ List<String> countyCombo = new List<String>(); List<String> county =new List<String>(); for(Id idsOfTerritory : territoryMarket.KeySet()){ if(territoryMarket.get(idsOfTerritory).County_State_Combo__c!=null) countyCombo.add(territoryMarket.get(idsOfTerritory).County_State_Combo__c); county.add(territoryMarket.get(idsOfTerritory).Territory_County__c); } List<Account> iterateAccount = [Select a.Market_Territory__c,a.Id,a.Name,a.County_and_State_Combo__c From Account a where (a.RecordType.Name =: dealerProspect or a.RecordType.Name=:formerCustomer) and a.ShippingState!=null and a.Shipping_County__c !=null and a.County_and_State_Combo__c in :countyCombo and a.Shipping_County__c in:county and a.RecordTypeId!=null and a.Territory_Affiliate__c like'%Cars.com%']; List<Territory_Market__c> newMarket = [select t.Parent_Market__r.Name,t.Parent_Market__r.Market_Category__c,t.County_State_Combo__c,t.Id From Territory_Market__c t where t.Id in: territoryMarket.KeySet()]; List<Account> updateAccount = new List<Account>(); for(Territory_Market__c market : newMarket){ system.debug('account size'+iterateAccount.size()); for(Integer i=0;i<iterateAccount.size();i++){ system.debug('name'+ market.Parent_Market__r.Name+','+ market.Parent_Market__c); String name = market.Parent_Market__r.Name; if(iterateAccount.get(i).County_and_State_Combo__c==market.County_State_Combo__c){ if(name!=null && name!=''&& name.contains('/')){ List <String> nameOfTerritory = name.split('/'); for(Integer j=0;j<nameOfTerritory.size();j++){ if(nameOfTerritory.get(j).contains('Cars.com')){ system.debug('found cars.com market territory'+nameOfTerritory.get(j)); if(nameOfTerritory.get(j).contains('Cars.com - National')){ iterateAccount.get(i).Market_Territory__c = 'Cars.com - National'; iterateAccount.get(i).Market_Category__c = 'I'; }else{ iterateAccount.get(i).Market_Territory__c = nameOfTerritory.get(j); iterateAccount.get(i).Market_Category__c = market.Parent_Market__r.Market_Category__c; } } } }else{ system.debug('found cars.com without / market territory'+name); if(name!=null && name.contains('Cars.com - National')){ iterateAccount.get(i).Market_Territory__c = 'Cars.com - National'; iterateAccount.get(i).Market_Category__c = 'I'; }else{ iterateAccount.get(i).Market_Territory__c = name; iterateAccount.get(i).Market_Category__c = market.Parent_Market__r.Market_Category__c; } } system.debug('account anme'+iterateAccount.get(i).name+'territory name'+name); updateAccount.add(iterateAccount.get(i)); } } } update updateAccount; }

 

I am getting error while updating parent market in Market Territory object.

 

Error:Apex trigger FindMarketTerritoryFromMarket caused an unexpected exception, contact your administrator: FindMarketTerritoryFromMarket: execution of AfterUpdate caused by: System.Exception: Too many DML rows: 244: Trigger.FindMarketTerritoryFromMarket: line 80, column 5

 

I already put update statement outside of for loop nad array list. Can some one point me some work around this?

 

Thanks

dmchengdmcheng

You're not giving much information here.  I assume you know that you've exceed the governor limit for DML statements, which is 100 * (number of records in the batch), so if the trigger fires for one territory market record, you can only update 100 accounts in a single statement.

 

If a change to a single territory market record can indeed affect hundreds of accounts, then you'll have to write code to divide the accounts into groups of 100 so you can run the update.  Check the documentation or other forum postings for examples.