+ Start a Discussion
nickwick76nickwick76 

Too many DML rows

Hi,

When the SOQL-query in the code below returns more than 100 records I get following error message:

 

System.LimitException: Too many DML rows: 184

 

public void rollupAccountLocationUpdate(List accountLocations) {
// Rollup updates to account locations to the opportunity line item "Last Modified Date" field    
        List<OpportunityLineItem> opportunityLineItemsToUpdate = new List<OpportunityLineItem>();
        for (OpportunityLineItem opportunityLineItem : [Select Id From OpportunityLineItem Where Account_Location__c In : accountLocations]) {
    	    opportunityLineItem.Last_Modified_Date__c = system.now();
    	    opportunityLineItemsToUpdate.add(opportunityLineItem);
        }
        Database.SaveResult[] results = Database.update(opportunityLineItemsToUpdate, false);
}

 

As I understand it, the update command should be able to take 1000 records.

This class is called by a trigger. Could that be the reason?

 

Ideas and suggestions are welcome!

 

BR / Niklas

 

Best Answer chosen by Admin (Salesforce Developers) 
sales4cesales4ce

You can update a max of 100 DML rows per transaction(assuming you have 1 record). This value scales with the number of records that needs to be updated.

If you have 2 records that needs to be updated, max DML rows limit would be 200.

 

Example:

 

If you are trying to update, assume  1 record of "Account_Location__c"  that has 184 child items, it throws "Max DML rows " error.

Because you can only update 100 DML rows per transaction.

(In your case, i believe 1 account_location__c has 184 line items).

 

Best way to overcome this is by using "@future" annotation on your class that can allow upto 10,000.

 

Hope this helps.

 

Sales4ce

 

All Answers

Imran MohammedImran Mohammed

In a single transaction upto 100 DML statements can be issued.

Even though the way you are doing is not  as per the best practices, but the code should for work fine.

Can you let me  know, is this particular method being invoked from another method?

nickwick76nickwick76

It is being invoked from a trigger:

 

trigger tgrAccountLocationAfterInsertUpdate on Account_Location__c (after insert, after update) {
   clsRollUpLastModifiedDate util = new clsRollUpLastModifiedDate();
   util.rollupAccountLocationUpdate(Trigger.new);     
}

Rgds / Niklas

 

Imran MohammedImran Mohammed

Trigger has different limitations. 

Your code should be as below

 

public void rollupAccountLocationUpdate(List accountLocations) {
// Rollup updates to account locations to the opportunity line item "Last Modified Date" field    
        List<OpportunityLineItem> opportunityLineItemsToUpdate = new List<OpportunityLineItem>();
        for (OpportunityLineItem[] opli : [Select Id, Last_Modified_Date__c From OpportunityLineItem Where Account_Location__c In : accountLocations]) 
        {
         for(OpportunityLineItem op: opli)
         {
        op.Last_Modified_Date__c = system.now();
        opportunityLineItemsToUpdate.add(op);
     }
        }
        Database.SaveResult[] results = Database.update(opportunityLineItemsToUpdate, false);
}

 

Let me know if you face any issues.

 

Imran MohammedImran Mohammed

Also, i will recommend you to go through the Governor limits in Apex documentation

nickwick76nickwick76

Thanks, but it's the same problem.

It complains on the Database.update row as before.

 

// Niklas

Imran MohammedImran Mohammed

In the controller code, can you make sure you have this parameter

public void rollupAccountLocationUpdate(Account_Location__c[] accountLocations) {

 

instead of this


public void rollupAccountLocationUpdate(List accountLocations) {

 

 

 

 

 

 

 

 

Imran MohammedImran Mohammed

 

If you are using just List as the type for the parameter, then update the code to this
public void rollupAccountLocationUpdate(Account_Location__c[] accountLocations) {
// Rollup updates to account locations to the opportunity line item "Last Modified Date" field    
ID[] idList = new ID[]{};
for(Account_Location__c al : accountLocations)
{
idList.add(al.id);
}
        List<OpportunityLineItem> opportunityLineItemsToUpdate = new List<OpportunityLineItem>();
        for (OpportunityLineItem[] opli : [Select Id, Last_Modified_Date__c From OpportunityLineItem Where Account_Location__c In : idList]) 
        {
         for(OpportunityLineItem op: opli)
         {
        op.Last_Modified_Date__c = system.now();
        opportunityLineItemsToUpdate.add(op);
     }
        }
        Database.SaveResult[] results = Database.update(opportunityLineItemsToUpdate, false);
}

 

sales4cesales4ce

You can update a max of 100 DML rows per transaction(assuming you have 1 record). This value scales with the number of records that needs to be updated.

If you have 2 records that needs to be updated, max DML rows limit would be 200.

 

Example:

 

If you are trying to update, assume  1 record of "Account_Location__c"  that has 184 child items, it throws "Max DML rows " error.

Because you can only update 100 DML rows per transaction.

(In your case, i believe 1 account_location__c has 184 line items).

 

Best way to overcome this is by using "@future" annotation on your class that can allow upto 10,000.

 

Hope this helps.

 

Sales4ce

 

This was selected as the best answer
nickwick76nickwick76

Thanks!

Got it now! I thought the limit was regarding the number of update calls.

The annotation @future is probably what I need. I saw that the method has to be static though so it requires some changes, but that should be possible to fix.

 

// Niklas