I have 2 batch Apex classes that I am trying to run sequentially. I updated my classes based on a post I saw using a custom setting (BatchSchedule) with 1 field (Scheduled_Id__c) that is et to 0. However, I am getting an error message on the 1st class in the sequence when the batched run:
Failed to process batch for class 'AccountTotalEncountersBatch' for job id '7071C0000BcIUR8'
caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Class.AccountTotalEncountersBatch.finish: line 36, column 1
Does anyone know how I can resolve this issue?
1st Batch Class:
public class AccountTotalEncountersBatch implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([SELECT Id FROM Account]);
}
public void execute(Database.batchableContext context, Account[] scope) {
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>([
SELECT Account__c Id, COUNT(Id) amt
FROM Encounters__c
WHERE Account__c = :scope AND Age__c<366 GROUP BY Account__c]);
for(Account record: scope) {
AggregateResult amount = results.get(record.Id);
if(amount != null) {
record.Encounters__c = (Decimal)amount.get('amt');
} else {
record.Encounters__c = null;
}
}
update scope;
}
public void finish(Database.BatchableContext bc) {
BatchSchedule__c b = BatchSchedule__c.getOrgDefaults();
DateTime n = datetime.now().addMinutes(2);
String cron = '';
cron += n.second();
cron += ' ' + n.minute();
cron += ' ' + n.hour();
cron += ' ' + n.day();
cron += ' ' + n.month();
cron += ' ' + '?';
cron += ' ' + n.year();
b.Scheduled_Id__c = System.schedule('Batch 2', cron, new AccountTotalReferralsSchedule());
update b;
}
}
1st Batch Class Scheduler:
global class AccountTotalEncountersSchedule implements Schedulable{
global void execute(SchedulableContext sc){
AccountTotalEncountersBatch ref = new AccountTotalEncountersBatch();
database.executebatch(ref,200);
}
}
2nd Batch Class:
public class AccountTotalReferralsBatch implements Database.Batchable<sObject> {
public Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator([SELECT Id FROM Account]);
}
public void execute(Database.batchableContext context, Account[] scope) {
Map<Id, AggregateResult> results = new Map<Id, AggregateResult>([
SELECT Account__c Id, COUNT(Id) amt
FROM Referral__c
WHERE Account__c = :scope AND Age__c<366 GROUP BY Account__c]);
for(Account record: scope) {
AggregateResult amount = results.get(record.Id);
if(amount != null) {
record.Referrals__c = (Decimal)amount.get('amt');
} else {
record.Referrals__c = null;
}
}
update scope;
}
public void finish(Database.BatchableContext bc) {
BatchSchedule__c b = BatchSchedule__c.getOrgDefaults();
System.abortJob(b.Scheduled_Id__c);
}
}