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, I have very little experience with batch Apex?

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

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;
    
    Referral__c testRef = new Referral__c();
    testRef.Name = 'Test';
    testRef.Account__c = account.Id;
    insert testRef;

    BatchSchedule__c batchSched = new BatchSchedule__c();
        batchSched.Scheduled_Id__c = '1';
    insert batchSched;
    
    AccountTotalReferralsBatch testBatch = new AccountTotalReferralsBatch();
    
    Database.executebatch(testBatch);        
    Test.stopTest();
}

public static testmethod void testBatch2() {
    Test.startTest();
    
    Account account = TestUtility.createAccount('Prospect', Constants.RECORDTYPEID_ACCOUNT_FAMILY);
    insert account;

    BatchSchedule__c batchSched = new BatchSchedule__c();
        batchSched.Scheduled_Id__c = '1';
    insert batchSched;
    
    AccountTotalReferralsBatch testBatch = new AccountTotalReferralsBatch();
    
    Database.executebatch(testBatch);        
    Test.stopTest();
}

public static testmethod void testSchedule() {
    Test.startTest();
    
    BatchSchedule__c batchSched = new BatchSchedule__c();
        batchSched.Scheduled_Id__c = '1';
    insert batchSched;
    
    AccountTotalReferralsSchedule testSched = new AccountTotalReferralsSchedule();
    String sch = '0 0 23 * * ?';
    system.schedule('Test status Check',sch,testSched);
    
    Test.stopTest();
}

}

1 Answer 1

1

You are populating BatchSchedule__c.Scheduled_Id__c with a value that is not an Id.

BatchSchedule__c batchSched = new BatchSchedule__c();
    batchSched.Scheduled_Id__c = '1';
insert batchSched;

Naturally when you ask the system to abort that nonexistent job, it fails:

        System.abortJob(b.Scheduled_Id__c);

You need to populate that setting with an actual scheduled job id, or ensure your code correctly handles a null value and leave it blank.

You also need to write meaningful assertions for your code. At present, these are "smoke tests" that provide very little value. See our canonical-qa tag for more great resources on writing unit tests.

6
  • Thanks. I did try entering the actual ID for the custom setting, but still get the same error. I do know I need assertions, I was just trying the get the smoke test to pass first.
    – J. Neilan
    Nov 12, 2020 at 16:23
  • 1
    You'd have to supply the Id of a scheduled job created inside the test context. You can't hard-code an id.
    – David Reed
    Nov 12, 2020 at 16:25
  • Oh right. So I would need to create the 1st scheduled job in my test class and then use the ID from that job?
    – J. Neilan
    Nov 12, 2020 at 18:06
  • 1
    That is correct.
    – David Reed
    Nov 12, 2020 at 18:10
  • Thanks. I added that in and now get another error. I've updated my post above. Any ideas?
    – J. Neilan
    Nov 12, 2020 at 18:20

You must log in to answer this question.

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