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
eyekameyeyekamey 

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?
{tushar-sharma}{tushar-sharma}
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.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
AbhishekAbhishek (Salesforce Developers) 
Hi,

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 = 'test@salesforce.com', Phone = '123456');
        count++;
        cons.add(c);
    }
    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,

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm


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

Thanks.
 
eyekameyeyekamey
@Tushar-sharma
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.
{tushar-sharma}{tushar-sharma}
Yes you can use it with custom ist as well. Check thread for reference: https://developer.salesforce.com/forums/?id=906F00000008yXCIAY