6

Does anyone know how does salesforce calculate DML rows? What is included to DML Rows Count?

Consider following code which hits "System.LimitException: Too many DML rows: 10001"

List<MyMisteriousSObject> myList = new List<MyMisteriousSObject>();
for ( Integer i = 0; i < 9996; i++) {
    myList.add( new MyMisteriousSObject() );
}

System.debug( 'myList.size() : ' + myList.size() ); // => here I see in debug myList.size() : 9996

insert myList; // => here it fails with "System.LimitException: Too many DML rows: 10001"

How this could be possible?

I believed that DML rows count should be equal to list size, shouldn't it?

2 Answers 2

6

The DML rows are counted against one transaction not per DML statement for example below code will work as myList.add( new Account(name='xyz') ); is commented. if you uncomment the statement then it will hit Too many DML rows limit. Somewhere in your code there is more than 4 DML exists apart from the code shown which makes a total more than 10000 to make the code fail

List<Account> myList = new List<Account>();
   // myList.add( new Account(name='xyz') );
   // myList.add( new Account(name='xyz') );
   // myList.add( new Account(name='xyz') );
   // myList.add( new Account(name='xyz') );
   // myList.add( new Account(name='xyz') );

for ( Integer i = 0; i < 9996; i++) {
    myList.add( new Account(name='xyz') );
}

System.debug( 'myList.size() : ' + myList.size() ); // => here I see in debug myList.size() : 9996

insert myList;
2
  • Gotcha. So I have put following line before my actual insert System.debug('Before insert. Current DML rows count: ' + Limits.getDMLRows() + ' of total: ' + Limits.getLimitDMLRows() ); insert myList; and have seen 'Before insert times. Current DML rows count: 27 of total: 10000' in log. It seems I have misunderstood conception of 'transaction', as for me, every DML statement is separate 'transaction'.
    – Patlatus
    Dec 31, 2013 at 10:59
  • Also, if you have a trigger on "MyMyseteriousObject" that also inserts records, then that too will count against the DML limit. So if I have 5,001 MyMysteriousObject records, but each of those create 1 MyMysteriousChild record in a trigger, then you will exceed the DML Limit as well. Dec 31, 2013 at 15:34
4

The DML rows count is across the whole unit of code being executed. Is the code snippet that you are showing all the code that runs?

If you insert 5 rows and then later in the code insert 9996 rows those are added together and you will exceed the limit.

It's the same as if you run multiple queries each less than 50,000 rows, you can still break the SOQL limit if the sum of those queried rows is too high.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .