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){ /* generate new account */ Contact c = new Contact(LastName = account.Name, accountid = account.id); insert c; } }
The preceding trigger will fail after processing...