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
SureshKolliSureshKolli 

How to write a Batch apex to insert records

Hi Friends 

Im Suresh new to SFDC .

Can anyone help me in writing an Batch apex class to insert records more than 500k.

Thanks in Advance

Suresh Kolli

hitesh90hitesh90

Hi Suresh,

 

Here is the Example of  Insert a large number of records, instead of operating on existing records.

Link:- Batch Apex to Insert a large number of records, instead of operating on existing records


Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

vbsvbs

@Suresh - Have you considered using apex data loader? You can set up a commandline script to import on a daily basis if required using scheduled jobs. I would really question use of batch apex for such large data volumes just for data insertion.

SureshKolliSureshKolli

Thank u all for ur response 

Hey Hitesh i do hav an doubt .

In the given link u have posted two classes one i related to  Iterable and the other was using batchable std format with 3 methods.

So in the given example we need to club both classes and execute or how does it work .

Can u plz help me out with an example as custom object.

 

 

I wil appreciate your help

Thanks in Advance

Suresh

hitesh90hitesh90

Hi,

 

Refer below Blog there is a an example same as your requirement.

Batch Apex & CSV Parsing

 

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,

Hitesh Patel

SFDC Certified Developer & Administrator

My Blog:- http://mrjavascript.blogspot.in/

nikkeynikkey

Hi All ,

I have a written a Batch Class to Insert the records .

But i cudnt see any records got inserted.

Can anyone help me in resolving this code.

i appreciate ur help.

global class inserta implements Database.Batchable<sobject>
{
global final String Query;
global final String Field;
global final String Entity;
global inserta(String q ,String f,String e)
{
Query = q;
Field = f;
Entity = e;
}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC , List<a__c> scope)
{
list<a__c> lista = new list<a__c>();
a__c a =new a__c();
integer i;
for(i=0 ; i<10000 ;i++)
{
//a = new (a__c);
lista.add(a);
//a.put(Field.Value);
}
insert lista;
}
global void finish(Database.BatchableContext BC)
{
//Send an email to the User after your batch complete
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
String[] ToAddresses = new String[] {'salesforcecrm@gmail.com'}; 
mail.setToAddresses(ToAddresses); 
mail.setSubject('Apex Batch Job is done for insertion'); 
mail.setPlainTextBody('The batch Apex job processed '); 
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

}