0

I have 2 batch Apex classes that update Account fields from different custom objects. I was running into issues of accessing records and found a way to run the batches in order. The test class for my 1st batch runs and covers my classes. When I run the 2nd test class, I get the following error & Stack Trace:

System.StringException: Only CronTrigger IDs and batch, future, and queueable job IDs are supported.

Class.AccountTotalReferralsBatch.finish: line 23, column 1

Can anyone help me resolve this error?

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

Scheduler Class:

global class AccountTotalReferralsSchedule implements Schedulable{
    global void execute(SchedulableContext sc){
        AccountTotalReferralsBatch ref = new AccountTotalReferralsBatch();
        database.executebatch(ref,200);
    }
}

Test Class:

@istest (SeeAllData=FALSE)
public class AccountTotalReferralsTest{
    
    public static testmethod void testBatch1() {
        Test.startTest();
        
        Account account = TestUtility.createAccount('Prospect', Constants.RECORDTYPEID_ACCOUNT_FAMILY);
        insert account;
        
        Contact contact = TestUtility.createContact(account.Id, Constants.RECORDTYPEID_CONTACT_FAMILY, 'Primary');
        insert contact;
        
        Encounters__c encounter = TestUtility.createEncounter(account.Id, contact.Id, '', '');
        insert encounter;
        
        BatchSchedule__c batchSchedEnc = new BatchSchedule__c();
        insert batchSchedEnc;
        
AccountTotalReferralsBatch();

            string chron = '0 0 23 * * ?';
            string jobid = System.schedule('testScheduledApex', chron, new AccountTotalEncountersSchedule());
            CronTrigger ct = [Select id , CronExpression from CronTrigger where id = :jobId];

        Referral__c testRef = new Referral__c();
        testRef.Name = 'Test';
        testRef.Account__c = account.Id;
        insert testRef;

        batchSchedEnc = [SELECT Id,Scheduled_Id__c FROM BatchSchedule__c WHERE ID =: batchSchedEnc.Id];
            batchSchedEnc.Scheduled_Id__c = ct.Id;
        
        AccountTotalReferralsBatch testBatch = new AccountTotalReferralsBatch();
        
        Database.executebatch(testBatch);        
        Test.stopTest();
    }
}
8
  • 1
    Do you have a typo where you are scheduling the class? In your test class I see AccountTotalEncountersSchedule whereas in the question you have it as AccountTotalReferralsSchedule. – Jayant Das Nov 12 '20 at 19:04
  • 1
    Does that custom setting field have a value? If so, what is it? – Adrian Larson Nov 12 '20 at 19:05
  • @JayantDas I have the AccountTotalEncountersSchedule class in there to create a batch record that the custom setting would access for the AccountTotalReferralsAchedule class – J. Neilan Nov 12 '20 at 19:11
  • 3
    @J.Neilan I could be reading this wrong, but did you just forget to add update batchSchedEnc; after the line batchSchedEnc.Scheduled_Id__c = ct.Id;? – Bow-chicawow-ers Nov 13 '20 at 3:45
  • 1
    Sure thing. As the lone developer at my company, love the virtual code review community we've got here – Bow-chicawow-ers Nov 13 '20 at 18:54
2

Looks like you're just missing update batchSchedEnc; after the line batchSchedEnc.Scheduled_Id__c = ct.Id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.