DML inside a loop

As in the case of the previous anti-pattern, Salesforce imposes another governor limit known as total number of DML statements issued in a single request. As the name implies, it restricts the number of Data Manipulation Language (DML) statements. For example, insert, update, delete, undelete, and so on.

Note

You can get a comprehensive list of all the DML statements in the official documentation at https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm.

Our business requirement is to create a contact for each account inserted. So, we developed our code, as follows:

trigger AccountCreation on Account (after insert) { /* Iterate over all accounts inserted */ for(Account account : Trigger.new){ ...

Get Apex Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.