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
ErikOSnet0ErikOSnet0 

Governor limits: 1,000 list elements and INSERTs

I understand the purpose of the governor limits.  I understand some of the limits themselves, such as heap size.  I do not understand many of them.  One I do not understand is a limit of 1000 for a List. It appears to be an indirect memory limit, which is a duplicate effort since there is already a heap limit, and counterintuitive since one element can be as small as an Integer or as large as a complex blob containing object. 

 

THE QUESTION:  If I need to insert 1600 rows, does this actually mean I have to create two lists, one with 1000 and another with 600 rows, which I then have to execute as two inserts?  Am I understanding this correctly?!?

 

Best Answer chosen by Admin (Salesforce Developers) 
erik-iterik-it
They have since removed the limit. I always argued it was the most absurd governor limit. And, since its only possible benefit can be to try to limit memory use, the heap limit already does this. Recently, two SFDC developers said that was the reason SFDC removed this limit. I suspect they did it in Spring '11 when they revamped gov limits. But, I didn't see this one in the release notes. I've learned those notes are not comprehensive and they tend not to mention improvements to very embarrassing things. This limit was embarrassing, as most languages in the 1980s didn't have such a limit.

All Answers

luckymeluckyme

I agree the 1000 limit on collection is stupid. It would force us to write ugly code like use several lists, or lists of lists.

 

aalbertaalbert

The "list" limit proportionally increases as the batch size increases. Meaning, if 50 records cause a trigger to initiate, the list limit increases to a max of 2000. Simply point it out - not saying it solves this specific issue.

 

Additionally, the insert DML operation only takes a max of 200 records at a time, so even with 1000 records, you would have to iterate across the set , for each 200. 

 

To get higher limits and more flexibility, I recommend looking at Batch Apex

TehNrdTehNrd

aalbert wrote:

Additionally, the insert DML operation only takes a max of 200 records at a time, so even with 1000 records, you would have to iterate across the set , for each 200. 

 


aalbert,

It would appear code like this works for me. I don't have to iterate over it and break it into chunks of 200 for insertion.

List<Account> accts = new List<Account>();
for(Integer i = 0; i < 1000; i++){
accts.add(new Account(Name = 'test' + i));
}
insert accts;

Message Edited by TehNrd on 10-29-2009 04:39 PM
aalbertaalbert

Interesting - thanks for the correction. That code does run.

 

The apex docs seem to be misleading (link) where it says only a max of 200 sObjects can be passed into a single insert method. Or I didn't understand it properly. Nonetheless, I stand corrected. 

TehNrdTehNrd
I understood the docs the same as you. They must be wrong.

At least I hope they are as we depend on the current behavior and changing that would not be good.
ErikOSnet0ErikOSnet0
What I gathered is from Apex Batch, your inserts could be broken down into 200 record chunks, for transactional purposes.  In other words, if you insert 1000 via Apex Batch, 600 could succeed, and 400 could fail, as the lattter 400 could run as separate batch processes.  I also saw a post indicating that order was not guaranteed, and these batches could run concurrently.  All of this is supposed to be handled in the background by Apex Batch.  Your code doesn't explicitly create 200 row sets. 
TerryLuschenTerryLuschen

What is the correct answer to this?  Is there a 1000 limit on lists or not?

 

I just ran this in the Execute Anonymous and it worked fine...
List<Integer> myList = new List<Integer>();

system.debug('Start');
for (Integer i = 0; i < 2000; i++){
    myList.add(i);
}
system.debug('All Done');

 

Or is the limit more specific to sObjects or the number of objects that you can insert at a time?

 

Thanks!

 

erik-iterik-it
They have since removed the limit. I always argued it was the most absurd governor limit. And, since its only possible benefit can be to try to limit memory use, the heap limit already does this. Recently, two SFDC developers said that was the reason SFDC removed this limit. I suspect they did it in Spring '11 when they revamped gov limits. But, I didn't see this one in the release notes. I've learned those notes are not comprehensive and they tend not to mention improvements to very embarrassing things. This limit was embarrassing, as most languages in the 1980s didn't have such a limit.
This was selected as the best answer
Dheeraj Sharma 9Dheeraj Sharma 9
@terryLuschen I went through above conversation and then I try to figure out the issue. So I just fire an insert operation on  list from Developer console.
and it seems like the limit is 10000 records at a time. 
List<Attachment> atts = new List<Attachment>();
for(integer i=0;i<10001;i++)
{
    Attachment objAttach = new Attachment();
 objAttach.Name = 'testDemo'+i;
    objAttach.contentType = 'text/plain';
    objAttach.ParentId='00Q9000000rssuB';
    objAttach.body = Blob.valueOf('attachment'+i); 
    atts.add(objAttach);
}
insert atts;

for the above code this below error occur 

Error message for DML

and when I canged the limit to 10000 its executed successfully.