1

After working on many different code bases in Salesforce, I noticed that almost all developers check if a list is empty before doing a DML in order to avoid the unnecessary DML statement.

Here's an example:

if(!accountList.isEmpty()) {
    update accountList;
}

However, I tried running the code below in Execute Anonymous:

Account[] accounts = new Account[]{};
update accounts;
System.debug(Limits.getDmlRows()); // Returns Zero
System.debug(Limits.getDmlStatements()); // Returns Zero
System.debug(Limits.getLimitDmlRows()); // Returns 10,000
System.debug(Limits.getLimitDmlStatements()); // Returns 150

Based on the results, I came to the conclusion that checking if a list is empty or not is unnecessary when it comes to Salesforce limits. It also does not fire any triggers, flows etc. on the Account object.

I guess the question is: Is there any other reasons to do the check before a DML or is this common trap we all fell into?

2
  • 1
    It is unnecessary as you have found.
    – Phil W
    Jan 21, 2023 at 12:52
  • 2
    It's only our fourth highest voted question on this site.
    – Adrian Larson
    Jan 21, 2023 at 15:54

1 Answer 1

4

Nope, there is zero benefit in doing an empty check on a list before performing DML on it.

There is some indication that Salesforce used to count the DML statement against you when the list was empty, but digging up information on when that was the case (or when it changed) has proven difficult. I can only say that this hasn't been a concern since at least late 2013. The actual date would be further in the past.

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