Do not #01

Illia Leshchuk
1 min readDec 24, 2021

If I got a penny every time I saw a check for an empty list is made before a DML operation…

List<SObject> records;
...
if (!records.isEmpty())
{
//a DML operation goes here, e.g. an update
update records;
}

I’m not sure when a change was made, but it’s been years since apex interpreter is smart enough to handle empty lists of records w/o biting off of any limits. So check it for yourself, increase API version if necessary, and do not do it anymore.

for (Integer i = 0; i < 1000; i++)
{
insert new List<Account>();
}
// Number of DML statements: 0 out of 150
// Number of DML rows: 0 out of 10000

PS: in a rare cases, when you deal with legacy code and it’s impossible to upgrade to a newer version of API — use a centralized place where you will perform such check, e.g. introduce DML executor class.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Illia Leshchuk
Illia Leshchuk

Written by Illia Leshchuk

A salesforce developer/architect

No responses yet

Write a response