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
John Neilan 18John Neilan 18 

Error with Test Class for Scheduled Batch

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


   
Best Answer chosen by John Neilan 18
John Neilan 18John Neilan 18
Swetha,

That was the same test class but a different error. I marked it as aswered on that thread and created a new thread because I go different errors.  I got an answer for this one. I forgot to add an update on line 30.

All Answers

SwethaSwetha (Salesforce Developers) 
HI John,
I see your query is being answered by David Reed on https://salesforce.stackexchange.com/questions/326205/error-in-test-class-for-scheduled-batch-apex

Please mark this answer as best to close the thread and help others facing the same issue
John Neilan 18John Neilan 18
Swetha,

That was the same test class but a different error. I marked it as aswered on that thread and created a new thread because I go different errors.  I got an answer for this one. I forgot to add an update on line 30.
This was selected as the best answer