function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ParidhiBindalParidhiBindal 

DML limit conflicting statements

I came across few statements which are not very clear to me and looks conflicting. Correct me if I am wrong –

1. Too many SOQL queries – 101 exception –
Reason - If there are more than 100 DML statements in one transaction/context. What does 1 transaction/context mean?
Does it mean within one class if I am trying to query 100+ DML statements? Is this is the only meaning or there is something else.

2. I tried to insert 160 records using lists to avoid the 101 exception. This code ran fine and records were created.
List<Account> listAccounts = new List<Account>();
For (Integer x = 1; x <=160 ; x++) 
{
   Account newAccount = new Account (Name='Loop MyAccount' + x);
   listAccounts.add(newAccount);
   System.debug('Account added ' + x);
}
insert listAccounts;

But I came through this URL - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples.htm

This URL mentions "If the list contains more than 150 items, the 151st update returns an exception that can’t be caught."
So now, I ran into confusion because the code had list of 160 records which is 150+ and it created 160 records for me.

Please let me know what part I am missing.

Thanks in advance.
 
ParidhiBindalParidhiBindal
Aslo, I read - Total number of DML statements issued limit is 150. What does this mean?
 
Vikash GoyalVikash Goyal
Hi Paridhi,

1. 1 transaction/context does not mean within one class, you can simply consider 1 transaction as a single operation or process. For example, In salesforce you are on an opportunity record edit page, you have updated a field and clicked on save. After saving there may be triggers which update other records like account, contact etc. So this whole process will be considered as a transaction not only saving opportunity.

2. Salesforce has a limit of 150 DML statements per transaction i.e. you can do max 150 DML operations (insert, update, delete, etc.) in a transaction.
In your case, you have added first all the Account records in a list then inserted list of accounts outside of 'for' loop. So this will be counted as 1 insert DML statement, not 160 DMLs. If you do the same thing using the following way then it will throw an exception after 150 records :

For (Integer x = 1; x <=160 ; x++) {
   Account newAccount = new Account (Name='Loop MyAccount' + x);
   insert newAccount;
}

Hope this will help you understand the above concept.

Thanks