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
Malik Butler 5Malik Butler 5 

Batch Job error

I have a batch job that is giving me an error when I use the execute anonymous window. Any ideas of what this coud be? 
global class ServiceAppointmentCancel implements Database.Batchable<SObject>, Schedulable{ global Database.QueryLocator start(Database.BatchableContext BC) { String subject = 'Reload'; String status = 'O'; Date scheduledEnd = Date.today(); String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment ' + ' WHERE subject = : ' + subject + ' AND status = :' + status + ' AND scheduledEnd < ' + scheduledEnd; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<ServiceAppointment> serviceAppointments) { // Loop to iterate over the service appointments for(ServiceAppointment service : serviceAppointments) { service.status = 'C'; } // Preforming the DML operation update serviceAppointments; } global void finish(Database.BatchableContext BC) { } global void execute(System.SchedulableContext SC) { Database.executeBatch(new ServiceAppointmentCancel(), 200); } }

String q = 'SELECT Reload, O, Date.today()'; Database.executeBatch(new ServiceAppointmentCancel(), 200);

User-added image
 
Best Answer chosen by Malik Butler 5
Abdul KhatriAbdul Khatri
Please try the below code. The only change I made is in the query String.
 
global class ServiceAppointmentCancel implements Database.Batchable<SObject>, Schedulable
{ 
	global Database.QueryLocator start(Database.BatchableContext BC) 
	{ 
		String subject = 'Reload'; 
		String status = 'O'; 
		Date scheduledEnd = Date.today(); 
		String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment WHERE subject = :subject AND status = :status AND scheduledEnd < :scheduledEnd'; 

		return Database.getQueryLocator(query); 
	} 

	global void execute(Database.BatchableContext BC, List<ServiceAppointment> serviceAppointments) 
	{ 
		// Loop to iterate over the service appointments 
		for(ServiceAppointment service : serviceAppointments) 
		{ 
			service.status = 'C'; 
		} 

		// Preforming the DML operation 
		update serviceAppointments; 
	} 

	global void finish(Database.BatchableContext BC) 
	{ } 

	global void execute(System.SchedulableContext SC) 
	{ 
		Database.executeBatch(new ServiceAppointmentCancel(), 200); 
	} 
}

 

All Answers

Abdul KhatriAbdul Khatri
What is the error are you facing?
Malik Butler 5Malik Butler 5
Abdul, it's the attachment at the very bottom. It says Fatal error and that the issue is in line 12 but I'm not sure where the error is.
Abdul KhatriAbdul Khatri
What is line no. 12 in your case. When I format your code. I get the following format
 
global class ServiceAppointmentCancel implements Database.Batchable<SObject>, Schedulable
{ 
	global Database.QueryLocator start(Database.BatchableContext BC) 
	{ 
		String subject = 'Reload'; 
		String status = 'O'; 
		Date scheduledEnd = Date.today(); 
		String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment ' + ' WHERE subject = : ' + subject + ' AND status = :' + status + ' AND scheduledEnd < ' + scheduledEnd; 

		return Database.getQueryLocator(query); 
	} 

	global void execute(Database.BatchableContext BC, List<ServiceAppointment> serviceAppointments) 
	{ 
		// Loop to iterate over the service appointments 
		for(ServiceAppointment service : serviceAppointments) 
		{ 
			service.status = 'C'; 
		} 

		// Preforming the DML operation 
		update serviceAppointments; 
	} 

	global void finish(Database.BatchableContext BC) 
	{ } 

	global void execute(System.SchedulableContext SC) 
	{ 
		Database.executeBatch(new ServiceAppointmentCancel(), 200); 
	} 
}

 
Malik Butler 5Malik Butler 5
"return Database.getQueryLocator(query);"
Abdul KhatriAbdul Khatri
Please try the below code. The only change I made is in the query String.
 
global class ServiceAppointmentCancel implements Database.Batchable<SObject>, Schedulable
{ 
	global Database.QueryLocator start(Database.BatchableContext BC) 
	{ 
		String subject = 'Reload'; 
		String status = 'O'; 
		Date scheduledEnd = Date.today(); 
		String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment WHERE subject = :subject AND status = :status AND scheduledEnd < :scheduledEnd'; 

		return Database.getQueryLocator(query); 
	} 

	global void execute(Database.BatchableContext BC, List<ServiceAppointment> serviceAppointments) 
	{ 
		// Loop to iterate over the service appointments 
		for(ServiceAppointment service : serviceAppointments) 
		{ 
			service.status = 'C'; 
		} 

		// Preforming the DML operation 
		update serviceAppointments; 
	} 

	global void finish(Database.BatchableContext BC) 
	{ } 

	global void execute(System.SchedulableContext SC) 
	{ 
		Database.executeBatch(new ServiceAppointmentCancel(), 200); 
	} 
}

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hey, did you try?
Malik Butler 5Malik Butler 5
Yes I did everything is working properly. Thanks so much for your help!