You need to sign in to do that
Don't have an account?

How to insert 500000 records into Object using Batch Apex
I'm New to Apex coding, I have requirement where I need to insert 500000 records into an object using Batch Apex.
It can be done via Data loader, but I wanted to try it using Batch Apex. guide me please.
Thanks in advance.
It can be done via Data loader, but I wanted to try it using Batch Apex. guide me please.
Thanks in advance.
Here is the sample code for inserting records. Please modify it according to your exact requirement.
Thanks & Regards,
Sameer Tyagi.
http://www.mirketa.com
If your data is alreaddy present in some other form in SalesForce you should use the apex batch, take a clue directly from salesforce help. If your data sits outside SalesForce use dataloader.
Thanks,
Yogesh
1) If you are trying to insert records using batch apex, given that you are having data stored in CSV files, you can use the below link and modify accordingly:
http://developer.financialforce.com/customizations/importing-large-csv-files-via-batch-apex/
2) If you are trying to insert records directly from an other system which has ability to do an API callout, you can use the Partner API Wsdl from salesforce and consume it in the application which can send call out! For getting Partner API WSDL, go to setup>develop>api>generate partner wsdl. For more info on consuming partner api, go throught the below link:
https://www.salesforce.com/developer/docs/api/Content/sforce_api_partner_examples.htm
You can also utilize rest api of salesforce to insert records from an external system.
3) You can also use ETL tools to do that work for you!
4) If you have the data in salesforce itsself and trying to massage the data and insert records into a different object , you can follow the example given by @Sameer Tyagi SFDC
Hope it helps,
Thanks,
Balaji
I was searching the same scenario for using batch to insert records but could not get any specific example for the insert scenario. After reading the salesforce help documentation I created following code and it worked for me. For insert operation I used Iterable<sObject> rather than Database.QueryLocator in start method:
public class VerifyBatchLimit implements Database.Batchable<sObject>, Database.Stateful {
// Variable to track processed transactions
public Integer recordsProcessed = 0;
public Iterable<sObject> start(Database.BatchableContext bc) {
List<POC_Employee__c> lstEmployee= new List<POC_Employee__c>();
POC_Employee__c empl;
for(Integer i=0; i<50000; i++)
{
empl= new POC_Employee__c();
empl.Employee_Name__c='Employee Name'+i;
lstEmployee.add(empl);
}
return lstEmployee;
}
public void execute(Database.BatchableContext bc, List<POC_Employee__C> employees){
// process each batch of records
System.debug(employees.size());
insert employees;
}
public void finish(Database.BatchableContext bc){
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
JobItemsProcessed,
TotalJobItems, CreatedBy.Email
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
System.debug(recordsProcessed + ': records processed!');
}
}
Hi samson,
Please find the solution.
BatchClassExample myBatchObject = new BatchClassExample();
Id batchId = Database.executeBatch(myBatchObject,200);