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
Ritika SinghRitika Singh 

Record count with DML

A developer in a Salesforce org with 100 accounts executes the following code using the Developer Console:
Account myAccount = new Account(Name=’MyAccount’);
insert myAccount;
  for(Integer x = 0; x < 150; x++) 
 {
  Account newAccount = new Account(Name=’MyAccount’ + x);
   try {
         insert newAccount;
         } catch(Exception ex) {
      System.debug(ex);
   }
}
insert new Account(Name='MyAccount');

How many accounts are in the org after this code is run? And why that count ?
A. 101
B. 100

C. 102
D. 252
Best Answer chosen by Ritika Singh
Agustin BAgustin B
Hi, you already have 100 records, in the code above you have one insert and after that 150 inserts which will pass the 150 limit (you have 151). None will be inserted because the transaction is not over. Thats why you only have the 100 you had before this code executed.

For example when you are testing apex code you have the Test.StartTest and Test.StopTest which gives you a new transaction with new limits so in a test you could insert 150 and then put another 150 inserts between Test.StartTest and Test.StopTest.

Hope it helps, if it helps please like and mark as correct, it may help others.

All Answers

Agustin BAgustin B
Hi, the answer is B as you have 150 dml statements limit. It will pass 150 inserts so no account will be inserted so you are left with your 100 initial accounts.

If it helps please like and mark as correct, it may help others with the same question.
Ritika SinghRitika Singh
Hi Agustin,

Thanks for the quick help ! Just to understand this much better, when you say " it will pass 150 inserts so no account will be inserted ". We know Limit of DML= 150, but here how we can say its passing 150 inserts and left with 100 records only?
Agustin BAgustin B
Hi, you already have 100 records, in the code above you have one insert and after that 150 inserts which will pass the 150 limit (you have 151). None will be inserted because the transaction is not over. Thats why you only have the 100 you had before this code executed.

For example when you are testing apex code you have the Test.StartTest and Test.StopTest which gives you a new transaction with new limits so in a test you could insert 150 and then put another 150 inserts between Test.StartTest and Test.StopTest.

Hope it helps, if it helps please like and mark as correct, it may help others.
This was selected as the best answer