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
textualtextual 

batch processing not working as expected

i need to process contacts and their trainings and need to do this via batch as its around 75k records when joined. this is a one time job so i was hoping to write a class and then call it via execute anonymous. in my testing on very small numbers in my sandbox, i only get the batch execution to run once, while im deliberately trying to get it to run more than once. for example, i have 25 records so i want to run in batches of 10 to run 3 times; 10, 10, 5. but it looks like it only runs once for 10 records. secondly, it looks like the query does not re-run on subsequent runs (if i even am running more than once). Meaning if the first 8 records are for one contact and the next 8 are for another, but i only run in a size of 10, the second contacts records will be split ovef the first run and second run. Im using a flag to set whether these records are 'counted' towards a certification but im guessing the records would be split rather than requery for records not flagged as being used. heres some code for partial example of how the basics are set up
 
global class Batch_TrainingLegacyBackfill implements Database.Batchable<sObject> {
	
	List<training_c> ContactTrainings;
	String query;
	
	global Batch_TrainingLegacyBackfill() {
		query = 'SELECT Id, Name, Certification__c, Chapter_Name__c, Course_Name__c, Complete_Date__c, Legacy_Processed__c, '  
					+ 'Contact__r.Id, Contact__r.Name, Contact__r.Email, Contact__r.Last_Training_Date__c , Contact__r.Number_of_Trainings__c ' 
					+ 'FROM training__c WHERE Legacy_Processed__c = null ORDER BY Contact__r.Id, Course_Name__c, Chapter_Name__c LIMIT 10';
	}
	
	global Database.QueryLocator start(Database.BatchableContext BC) {
		system.debug('query = ' + query);
		return Database.getQueryLocator(query);
	}

   	global void execute(Database.BatchableContext BC, List<sObject> scope) {
   		system.debug('scope = ' + scope);
		// grab records
		ContactTrainings = (List<training__c>)scope;

		for(training__c c : ContactTrainings){ // more logic than id care to admit...}

	}
	
	global void finish(Database.BatchableContext BC) {
		// email to verify
		
	}
}
//from execute anonymous

Batch_TrainingLegacyBackfill bbf = new Batch_TrainingLegacyBackfill();
Database.ExecuteBatch(bbf);


to recap:
1) if i have 5 contacts with a total of 25 records and have my limit set to 10, should this run 3 times?
2) can i get this ti run multiple times from execute anonymous
3) if it runs multilpe times, would it re-query everytime to bring back overlapping contact trainings?
Swati GSwati G
You should write batch for contact records and you can also fetch training in sub query if its count is less than 200. 

 
textualtextual
is there a reason to not use joined data? im a little bummed i gotta re-write this thing but if that will work in seeing it run more than once, ill do it
kanchi chandra mohankanchi chandra mohan
you can set the batch size can specify the excutebatch() method, this is specify the scope size not set the limit 10. batchs are start method execute only one time not set the batch size in start methods it is only excution time only