function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RossGRossG 

too many dml rows error?

Hey guys-

 

I'm getting an error in testing "too many dml rows".  This is just an after update trigger that calls a class that will insert a custom object record when an opp gets updated to meet some criteria.

 

I think the error is due to me having a for loop in the trigger, then calling a class that has a for loop in it, but I'm not sure how to resolve this.

 

trigger:

 if (Trigger.isAfter) {

 
            for(Opportunity opp5:  Trigger.new) 
                if(Trigger.isUpdate && opp5.Threshold__c=='1' && Trigger.oldMap.get(opp5.Id).Threshold__c!='1')

                {
                   ServicesEngagementManager seFromOpp = new  ServicesEngagementManager();
                   seFromOpp.createEngagement2(Trigger.new);
                 }        

 Anyone see how to fix this issue?  The trigger works in sandbox but i can't move to prod bc of this too many dml row issue on bulk testing

 

 

class:

    public void createEngagement2 (List<Opportunity> oppsFromTrigger)
    {
        List< SFL5_Projects__c> engagementList = new List<SFL5_Projects__c>();
        for(Opportunity currentOpp: [SELECT Id,Services_to_be_Delivered_by__c,Owner.Id,Probability,Threshold__c,Account.Id,Account.Name,Region__c,
                                                       CreatedById,Account.Region__r.OwnerId,Account.Region__r.RSA_Manager__r.Id,Professional_Services__c, 
                                                       Name From Opportunity WHERE id IN: oppsFromTrigger])
        {  
            SFL5_Projects__c se = new SFL5_Projects__c();
                if(currentopp.Account.Region__r.RSA_Manager__r.Id!=null &&  currentopp.Account.Region__c !=null)
                {
                se.OwnerId = currentopp.Account.Region__r.RSA_Manager__r.Id;
	        } else
                {
		se.OwnerId = currentOpp.Owner.Id;
		}
                se.Name = 'Services Engagement for: '+ currentOpp.Account.Name;
                se.Opportunity__c = currentOpp.Id;
                se.Client__c = currentOpp.AccountId;
                se.Region__c = currentOpp.Region__c;
                se.Region__c = currentOpp.Region__c;
                se.Project_Status__c = 'Active';
                se.Services_Amount__c = currentOpp.Professional_Services__c;
                se.Services_to_be_Delivered_by__c = currentOpp.Services_to_be_Delivered_by__c;
                engagementList.add(se);
                 
         }
    Database.insert(engagementList);
    }

 

TomSnyderTomSnyder
if (Trigger.isAfter) {

 List<Opportunity> filteredOps = new  List<Opportunity>();
 for(Opportunity opp5:  Trigger.new) {
    if(Trigger.isUpdate && opp5.Threshold__c=='1' && Trigger.oldMap.get(opp5.Id).Threshold__c!='1') {
         filteredOps.add(opp5);
          }
}
 ServicesEngagementManager seFromOpp = new  ServicesEngagementManager();
 seFromOpp.createEngagement2(filteredOps);
}  

 You are putting dml in a loop, do something like above or move your Threshold__c change logic filter inside your helper class and pass all the opps if that makes more sense.

 

souvik9086souvik9086

Never do DML operation or write SOQL query within for loop. It is not a best practice. Try to follow the above method mentioned by Tesii to add those opportunity ids' in a list and then call the class outside for loopr so that the insert will occur outside the for loop the error limit.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Avidev9Avidev9

too many dml rows error : this error generally happens if you are effecting more than 10K records in a transactions. More precisely doing DML on 10K records.

 

Due to inefficient use of loops you seek to be doing DMLs on all the records in evey iteration and it piles up. Well go through the bulkifing process the end result will look something lik tesil's code