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
Alan Mc CarthyAlan Mc Carthy 

Why can a DML statement insert a list of SObjects with 300 elements, but you can't make 300 DML statements simultaneously?

Hi folks,
I am asking about the theory involved here. What is the real difference between inserting a list of SObjects, and inserting Sobjects individually? Surely as long as the same number of records is going into the database, the operations should consume the same resources. For some code to show what I mean:
// Good
List<Contact> myContacts = new List<Contact>();
for( Integer i = 0; i < 1000; i++) {
    Contact myContact = new Contact(LastName = 'John');
    myContacts.add(myContact);
insert myContacts;


// Bad
for( Integer i = 0; i < 1000; i++) {
    Contact myContact = new Contact(LastName = 'John');
    insert myContact;
So in the code the 'Good' example will avoid governer limits and the 'Bad' example will run into problems... but aside from the governer limits the database would end up identical in each example. So what exactly is going on here?

Thanks for you help!
 
sfdcMonkey.comsfdcMonkey.com
first of all in both example close the for loop properly:
updated code:
// Good
List<Contact> myContacts = new List<Contact>();
 for( Integer i = 0; i < 1000; i++) {
    Contact myContact = new Contact(LastName = 'John');
    myContacts.add(myContact);
 }

 insert myContacts;


// Bad
for( Integer i = 0; i < 1000; i++) {
    Contact myContact = new Contact(LastName = 'John');
    insert myContact;
}

Now, as you can seen in the good example we are process 1000 record inside a for loop and add every records in List of contact List<Contact> and after the for loop process it will be make only 1 Insert DML call to server for insert the list,

 and in you bad example we are call insert DML inside the for loop it's means this will make 1000 insert DML call to server and this will be throw error

i hope you understand the difference between good and bad example
 Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks
sfdcmonkey.com
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
For your question,  i would like to give this analogy.  For a 10$ worth product is it better to insert a 10$ bill into vending machine or 10 $1 bills(assuming a customer has 10$ bill and 10 1$ bills in his/her pocket/wallet). Which is optimized method and which saves a customer time at commonly shared (Multi) tenant environment. And what Piyush Soni explained sufficies technical explanation. Hope this helps in clear the confusion.

Regards!
Alan Mc CarthyAlan Mc Carthy
Hi guys, thanks for your responses, but I was looking for a more technical answer. I understand that you are forbidden to make 1000 calls to the server. I want to know why. What is happening in a call to a server that makes it so computationally expensive that Slaesforce would limit it?

So @piyush, could I ask you to expand more? Why is 1000 calls to the server which insert one record each more expensive than one call to the server which inserts 1000 records?

Do you have any documentation about what is happening when a DML statement is made that could help me to understand?

Thanks.