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
Naomi HarmonNaomi Harmon 

Troubleshooting Batch Apex Class and Test Class

I am trying to create a batch Apex class that I can schedule to run weekly to automatically unqualify Leads that are 60 days old or older and haven't had activity on them in the past 10 days (and only for a single Record Type). Here is my Class and Test Class...My assertion keeps failing. It seems 0 of my leads are being updated correctly.

Batch Apex Class:
global class AutoUnqualifyLeads implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id, Status, Reason_Not_Qualified__c, LastActivityDate '+
                         'FROM Lead '+
                        'WHERE CreatedDate < LAST_N_DAYS:60 '+
                          'AND Days_Since_Last_Activity__c > 10 '+
                          'AND IsConverted = false '+
                          'AND Status != '+' \'Unqualified\' '+
                          'AND RecordTypeId = '+' \'0121I000000QxE7QAK\' ';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Lead> scope){
        List<Lead> leads = new List<Lead>();
        for (Lead l: scope){
                l.Status                  = 'Unqualified';
                l.Reason_Not_Qualified__c = 'No Response';
                system.debug(l);
                leads.add(l);
        }
   update leads;
   }

    global void finish(Database.BatchableContext bc){
        
    }
}

Test Class (my test load data information is listed below the code snippet):
@isTest

private class AutoUnqualifyLeadsTest {
    @isTest
    static void setupTest(){
        List<Lead> leadLoad = Test.loadData(Lead.sObjectType,'testLeadsAutoUnqual');
        System.debug(leadLoad);
        
    }
    
    @isTest
    static void testLeads(){
        
        Test.startTest();
        AutoUnqualifyLeads aul = new AutoUnqualifyLeads();
        Id batchId = Database.executeBatch(aul);
        Test.stopTest();

     System.assertEquals(3, [select count() from Lead where Status='Unqualified']);
    }
 
}
Test data:

CreatedDate, RecordTypeID, Days_Since_Last_Activity__c, Status, Reason_Not_Qualified__c, LastName, Company
2017-10-07T01:40:39, 0121I000000QxE7, 12, Open, Bing, Friends Co.
2017-09-22T01:43:39, 0121I000000QxE7, 13, Contacted, Gellar, Friends Inc.
2014-05-02T02:43:39, 0121I000000QxE7, 4, Open, Green, Friends LLC

Can you please help me troubleshoot??
 
Best Answer chosen by Naomi Harmon
Marcelo CostaMarcelo Costa

Hi...
Feels like yopu have the wrong annotation on line 4...
If that is where tou are setting the leads, the annotation should be @testSetup to load the leads so that the testLead methos have some test data.

class testLeads on line 12 has no data to test, that is why your asertion is failing...

All Answers

Marcelo CostaMarcelo Costa

Hi...
Feels like yopu have the wrong annotation on line 4...
If that is where tou are setting the leads, the annotation should be @testSetup to load the leads so that the testLead methos have some test data.

class testLeads on line 12 has no data to test, that is why your asertion is failing...

This was selected as the best answer
Naomi HarmonNaomi Harmon
Thank you so much Marcelo! That fixed it. :)