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

how to insert 100k records without a batch job

I need to create a custom mass upload tool that allows a user to upload a csv file and create contacts and leads based on some logic. The file can have up to 100,000 rows. This tool needs to be a real time, meaning once the user uploads the file, the records need to be created and the user cannot wait. What would be the best way to do this with Apex code? Can databse.insert handle inserting upto 100k records?
No, Without batch/future its not possible. We have standard limit of 10K record insingle transaction. We cannot bypass it. So you need batch here. You can check custom iterator in batch.

If this answer helps you, please mark it as accepted.

Tushar Sharma
AbhishekAbhishek (Salesforce Developers) 

You are trying to insert more than the Max limit allowed by Salesforce. The max number of records in a DML statement is 10000. You can change your logic to insert 10000 per call. Maybe add one outer loop like this

Code Snippet:-
for (integer j = 0; j < 10; j++) {
    List<Contact> cons = new List<Contact>();
    for (integer i = 0; i < 10000; i++) {
        Contact c = new Contact(LastName = 'test' + i, Email = '', Phone = '123456');
    try {
        Database.SaveResult[] result = database.insert(cons);
    } catch (exception e) {
        system.debug('Exception:' + e.getMessage());

You can check the blog which will let you know the salesforce Offical limits,

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Doesn't batch work only for Salesforce records? I cannot run a query to locate the records, because I don't have the records in Salesforce yet. I am processing the CSV file to insert the records into Salesforce. 
Derrick AbbeyDerrick Abbey
Hi EyeKamey,

The other two posts are correct that you would need batch to process that many records.  To answer your question, batch Apex can run on any iterable, not just Salesforce records.  So you can pass in any array and have it run on those.

Alternatively, have you considered creating a trigger to execute your logic that needs to run when inserting the records?  Then you could use a tool like dataloader to insert the records, and the trigger would execute the logic.
Yes you can use it with custom ist as well. Check thread for reference: