Using batch class i would like to insert records 10000 records in account and 10000 records in contact .so how many batch classes wil u write? can anyone help me on this synario?
Please confirm you want to insert or update your 10000 contact and account records?
if you want to update records, then you can write only one batch class.
maximum batch size could be 200 records.
global class UpdateAccountContactimplements Database.Batchable<sObject>{
global ExampleBatchClass(){ // Batch Constructor }
// Start Method global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator('Select id, accountid, name from contact limit 100000'); //query fields and filter records according to your choice }
// Execute Logic global void execute(Database.BatchableContext BC, List<Contact>conlist){
list<account> acclist = new list<contact>(); set<id> accidset = set<id>(); for(Contact con : conlist){ //so some change here
accidset.add(con.accountid); } update conlist; //update accounts records list<account> acclist = [select id , name from accounts where id IN: accids]; for(account acc : acclist){ // Do some change in account } update acclist; global void finish(Database.BatchableContext BC){ // Logic to be Executed at finish } }
You can query on Account also in start method and change logic accordingly in execute method.
I need to know the exect requirment to insert records through batch class so that I can provide you exact solution. because it is mandatory to query any of the object in Start method. Why you are not doing it through data loader or import wizard. However it is possible to insert record through batch class also.
May I know the exact requirement and the object you are quering in Start method?
Here is the sample code for inserting records.
global class InsertAccountContactimplements Database.Batchable<sObject>{
global ExampleBatchClass(){ // Batch Constructor }
// Start Method global Database.QueryLocator start(Database.BatchableContext BC){ String Query = ''; // Generate your string query on any object here with limit 10000 return Database.getQueryLocator(Query); //Query is Required on object which you want to run Batch }
// Execute Logic global void execute(Database.BatchableContext BC, List<sObject>objlist){ List<Account> acclist = new List<Account>(); list<Contact> conlist = new list<contact>() for(Sobject obj: objlist){ Account acc = new account(); acc.name = 'testname'; acclist.add(acc); } insert acclist; for(Account acc : Acclist){ Contact con = new Contact(); con.lastname = 'testname'; con.accountid = acc.id; conlist.add(con); } Insert conlist; global void finish(Database.BatchableContext BC){ // Logic to be Executed at finish } }
You can insert 100000 contact and account record in same batch class, But it also depends on your requirment. You can use data loader or import wizard for the same. Data Loader autometically inserts records in batches.
Hi Sayyad, You can simply create a CSV file in your system and put 10000 or more records then you will install Data loader software in your PC Now will login in Data loader with your SF credentials Select account object to insert. Map your CSV column name with Salesforce fields. and Insert record. It will provide you a success files with account id column Insert your 10000 Contact records and put the id of account in account id column and insert contacts using data loader.
Here is the detail info of data loader. https://developer.salesforce.com/page/Data_Loader
Read this : http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
And start developing your batch.
There is a governor limit of 10000 DML rows per apex invocation
But in Batch Class Every execute method run is treated as a seperate invocation
Please confirm you want to insert or update your 10000 contact and account records?
if you want to update records, then you can write only one batch class.
global class UpdateAccountContactimplements Database.Batchable<sObject>{
global ExampleBatchClass(){
// Batch Constructor
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('Select id, accountid, name from contact limit 100000'); //query fields and filter records according to your choice
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Contact>conlist){
list<account> acclist = new list<contact>();
set<id> accidset = set<id>();
for(Contact con : conlist){
//so some change here
accidset.add(con.accountid);
}
update conlist;
//update accounts records
list<account> acclist = [select id , name from accounts where id IN: accids];
for(account acc : acclist){
// Do some change in account
}
update acclist;
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
You can query on Account also in start method and change logic accordingly in execute method.
I need to know the exect requirment to insert records through batch class so that I can provide you exact solution. because it is mandatory to query any of the object in Start method.
Why you are not doing it through data loader or import wizard.
However it is possible to insert record through batch class also.
Let me know if above solution works for you .
Thanks & Regards,
Sameer Tyagi.
http://www.mirketa.com
May I know the exact requirement and the object you are quering in Start method?
Here is the sample code for inserting records.
global class InsertAccountContactimplements Database.Batchable<sObject>{
global ExampleBatchClass(){
// Batch Constructor
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
String Query = ''; // Generate your string query on any object here with limit 10000
return Database.getQueryLocator(Query); //Query is Required on object which you want to run Batch
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<sObject>objlist){
List<Account> acclist = new List<Account>();
list<Contact> conlist = new list<contact>()
for(Sobject obj: objlist){
Account acc = new account();
acc.name = 'testname';
acclist.add(acc);
}
insert acclist;
for(Account acc : Acclist){
Contact con = new Contact();
con.lastname = 'testname';
con.accountid = acc.id;
conlist.add(con);
}
Insert conlist;
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
You can insert 100000 contact and account record in same batch class, But it also depends on your requirment.
You can use data loader or import wizard for the same.
Data Loader autometically inserts records in batches.
Thanks & Regards,
Sameer
how can i achieve this with dataloader.
You can simply create a CSV file in your system and put 10000 or more records
then you will install Data loader software in your PC
Now will login in Data loader with your SF credentials
Select account object to insert.
Map your CSV column name with Salesforce fields.
and Insert record.
It will provide you a success files with account id column
Insert your 10000 Contact records and put the id of account in account id column
and insert contacts using data loader.
Here is the detail info of data loader.
https://developer.salesforce.com/page/Data_Loader
Sameer Tyagi.