+ Start a Discussion
Sanjeev R 1Sanjeev R 1 

Batch Processing with List collection for Custom object

I have a visualforce page that is having a file uploader on it and it take a CSV file, as this CSV file can have large amount of data, and have to perfrom some bussiness logic to data before importing it into SFDC therefore I have written controller class to it. After performing all operations I have List Collection for my custom object..

List<Credit_Item__c> lstCrI;

nameFile = contentFile.toString();
filelines = nameFile.split('\n');
lstCrI = new List<Credit_Item__c>();

 for (Integer i=1;i<filelines.size();i++)
      // extract data here from each row of CSV     

      Credit_Item__c objCr = new Credit_Item__c(); 

      // business logic here and assignment of "objCr "


            insert lstCrI;    
        catch (Exception e)
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the and try again later');

Now this may create problem in data in List collection is huge...
insert lstCrI;

Therefore I need to have a btach here on this list collection..

Requeting to please guide me for this as I m getting examples that the querrying things directly from database...in start() method, as I have a list collection here that I need to process in Batches

thanks in advance

James LoghryJames Loghry

A batch class is designed to work with either a Database.QueryLocator reference, for use with SOQL queries, or an Interable class such as a List of sobjects or a custom Iterable Apex class.

You'll want to create an additional batch class that takes either an Iterable class or a List of Credit_Item__c and operates over that list.  Here's an example of what your batch class might look like:

global class CreditItemBatch implements Database.Batchable<sObject>{

	public List<Credit_Item__c> creditItems {get;set;}

	global CreditItemBatch() {}

	global List<Credit_Item__c> start(Database.BatchableContext BC) {
		return creditItems;

   	global void execute(Database.BatchableContext BC, List<Account> scope) {
   		//Do stuff with the scope here....

	global void finish(Database.BatchableContext BC) {}

 For an example of a progress bar to show the progress of your batch job in a VF page, take a look at tehnerd's example here: http://www.tehnrd.com/batch-apex-status-bar/