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();
}
}
AccountTotalEncountersSchedule
whereas in the question you have it asAccountTotalReferralsSchedule
.update batchSchedEnc;
after the linebatchSchedEnc.Scheduled_Id__c = ct.Id;
?