+ Start a Discussion
Jason Kuzmak 12Jason Kuzmak 12 

Send bulk incoming records to batch apex

Hi all,

I have a ton of code that parses a CSV file of accounts coming from another database (via email services). These sheets can be quite large, and I thought it would be a good time to experiment with apex batch processing.

The trouble is, it looks like all of the examples of apex batches involve a query in the Start method. I don't need to query my accounts; I already have everything broken up into lists, and I want all of the dml for these records to occur in smaller batches.
Can this be done via batch apex, or is some other method better?
Ashish KumarAshish Kumar
No it is not necessary to put a query in batch.. However You can take advantage of iterable interface here.
You can pass the list of records to the batch and batch will work on those records only.
Here is an example for your reference.
global class batchClass implements Database.batchable{ 
   global Iterable start(Database.BatchableContext info){ 
       return new CustomAccountIterable(); 
   }     
   global void execute(Database.BatchableContext info, List<Account> scope){
       List<Account> accsToUpdate = new List<Account>();
       for(Account a : scope){ 
           a.Name = 'true'; 
           a.NumberOfEmployees = 70; 
           accsToUpdate.add(a); 
       } 
       update accsToUpdate; 
   }     
   global void finish(Database.BatchableContext info){     
   } 
}

Link for your reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Regards,