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
Jerry HeJerry He 

How can I insert large data using batch apex?

I am a new learner about apex, so I have some problem when I insert some data to object.

The main progress for inserting large data is below.(Now I can insert 50000 records,but I can not insert much more).

 

In a VF page, I make a button to call the batch apex class.

for(integer i = 0;i<5;i++){

    BatchApexTest batchApex = new BatchApexTest();

    String jobId = Database.executeBatch(batchApex );

}

In batch apex class's execute method, I write these.

List < MyObject > objList = new List<MyObject>();

MyObject obj;

Integer i;

for(i=0;i<10000;i++){

    obj = new MyObject(****);

    objlist.add(obj);

}

insert(objlist);

Through this method, I can insert 50000 records, but I can not insert much more.

From the salesforce web site, I have got that the batch apex can deal with 50 million records, how can I do this?

Please help me.

 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Not sure why you would want to create 50,000 or more records via batch apex but hey....

 

You can only have 5 batch jobs active at any given time. Since calling the batch in a for loop, you will not be able to do more than 5 batches of 10,000 records and thus 50,000 total. There are reasons for this, mainly due to sthe shared nature of SF.

 

There is one way around this and invloves writing an inbound email handler that will process an email sent by the batch after it finishes. The email handler will see the email and execute another batch. Modify as needed.

 

 

All Answers

Starz26Starz26

You would be best to use data loader if loading more than 50,000 records

Jerry HeJerry He

Thans for you suggestion, but now I just want to do it through apex code, do you have any idea to help me ?

Starz26Starz26

Not sure why you would want to create 50,000 or more records via batch apex but hey....

 

You can only have 5 batch jobs active at any given time. Since calling the batch in a for loop, you will not be able to do more than 5 batches of 10,000 records and thus 50,000 total. There are reasons for this, mainly due to sthe shared nature of SF.

 

There is one way around this and invloves writing an inbound email handler that will process an email sent by the batch after it finishes. The email handler will see the email and execute another batch. Modify as needed.

 

 

This was selected as the best answer
Jerry HeJerry He

Thanks for your help.

nagalakshminagalakshmi

Hi Jerry,

 

Did you get the solution for this. I am also facing the same problem while inserting the records in to object if the list having morethan 50000 records. i am able to insert the records up to 50000 successfully through batch apex. Please help me if you soved this issue.

 

Thanks,

Lakshmi

Jerry HeJerry He

It is also use batch method to insert large data no more than 5 million.

In Database.getQueryLocator method, that you can use a limit number ,for example

select id from object where condition limit 5000,

Ok, That we call batch method in apex code , we use Database.executeBatch(batchClass, number);

Ok, you will execute 5000/number times in this operation.

If you want to insert 500000, you can make the number equals 100, than in batch class, you can insert 10000 per time.

Is my answer can solve your question?