How to work efficiently with DML statements in Apex class

The following Apex DML statements are available:
Insert
Update
Upsert
Delete
Undelete
Merge

Salesforce allows only 150 DML statements for each Apex transaction. So its better to check the list against null or empty before performing DML operations. 

Example-

Account a = new Account(Name='Acme2');
insert(a);

Account myAcct = [SELECT Id, Name, BillingCity FROM Account WHERE Id = :a.Id];
myAcct.BillingCity = 'San Francisco';

try {
if(!myAcct.Empty()) { //Here if myAcct list is empty, you will save 1 DML operation.
update myAcct;
}
} catch (DmlException e) {
// Process exception here
}

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply