0

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);
    }         
}

1 Answer 1

1

Two things:

  1. You are calling getOrgDefaults() and then later update against the result. But if the default hasn't yet been inserted you get this error. To fix, simply change update to upsert.
  2. Just use the scheduleBatch method instead, which is much simpler than managing cron expressions.

Updated Code:

public void finish(Database.BatchableContext context)
{
    BatchSchedule__c config = BatchSchedule__c.getOrgDefaults();
    b.Schheduled_Id__c system.scheduleBatch(new Batch2(), 'unique name', 2);
    upsert config;
}
1
  • Thanks. I made those changes but now get a different error on my other batch. I'll put in a different post.
    – J. Neilan
    Commented Nov 17, 2020 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .