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
Charni WigginsCharni Wiggins 

Variable does not exist error for batch job?

I am trying to write a batch job that will take values from one field (if another date field falls in the previous month) and populate a new field on the same object, account. However I am getting the error: Variable does not exist: lastRefreshedDate (it is probably something really simple - not the most confident person in Apex). Thanks

Class

global class BatchCreditSafeDelta implements Database.Batchable<SObject> {
    
    DateTime lastRefreshedDate = System.Today().addMonths(-1);

	public static void runBatch() {
        BatchCreditSafeDelta batch = new BatchCreditSafeDelta();
        Database.executeBatch(batch);
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            [SELECT Id, Name, bottomline__creditSafeScore__c, bottomline__creditSafeLastRefreshed__c, last_month_creditSafe_Score__c 
			FROM Account WHERE bottomline__creditSafeLastRefreshed__c = LAST_MONTH ]);
    }

    global void execute(Database.BatchableContext bc, List <Account> scope) {
        List <Account> populateLastMonthScores = new List <Account>();
        for (Account acc: scope) {
            populateLastMonthScores.add(new Account(
                id = acc.id,
                Last_Month_CreditSafe_Score__c = acc.bottomline__creditSafeScore__c));
        }
        update populateLastMonthScores;
    }
    global void finish(Database.BatchableContext bc) {}
}

TestClass

@isTest public class BatchCreditSafeDeltaTest {
    @isTest static void batchTest() {
        
        sObjectFactory f1 = new sObjectFactory();
        Map<Id, Account> Accounts = new Map<Id, Account>();

        for (Integer i=0; i<10; i++) {
            Account a = (Account) f1.get( new Account(
                name = 'Account ' + i,
                bottomline__creditSafeScore__c = i,
                Last_Month_CreditSafe_Score__c = null,
                // change to one month before today
                bottomline__creditSafeLastRefreshed__c = lastRefreshedDate
            ));
            Accounts.put(a.id, a);
        }

        Set<Id> ids = Accounts.keyset();

        Test.startTest();
            BatchCreditSafeDelta.runBatch();
        Test.stopTest();

        Map<Id, Account> Accounts2 = new Map<Id, Account>();
        for (Account a : [SELECT Id, Name, Last_Month_CreditSafe_Score__c, bottomline__creditSafeLastRefreshed__c, bottomline__creditSafeScore__c,
                            creditSafe_Score_delta__c FROM Account WHERE ID IN :ids])

        {
            System.assertEquals( a.Last_Month_CreditSafe_Score__c, a.bottomline__creditSafeScore__c);
            System.assertEquals( a.CreditSafe_Score_Delta__c != null,  a.CreditSafe_Score_Delta__c != null);
        }       
    }
}
Asif Ali MAsif Ali M
Are you getting this error in your TestClass (line #13)?
Charni WigginsCharni Wiggins
Hi Asif, I have just changed the code, took out the variable and put it straight to assigning it to the fields value, seems to have cleared it now. But it lends me to ask the question - where would I declare a variable I only want to use in the Test class and not in the class? If possible.. Thank you
Charni WigginsCharni Wiggins
yes it was line 13!
Maharajan CMaharajan C
Hi Chami,

Try the below:

@isTest public class BatchCreditSafeDeltaTest {
    @isTest static void batchTest() {
        
        sObjectFactory f1 = new sObjectFactory();
        Map<Id, Account> Accounts = new Map<Id, Account>();
        Date lastRefreshedDate = System.Today().addMonths(-1);  // Date type right.
        for (Integer i=0; i<10; i++) {
            Account a = (Account) f1.get( new Account(
                name = 'Account ' + i,
                bottomline__creditSafeScore__c = i,
                Last_Month_CreditSafe_Score__c = null,
                // change to one month before today
                bottomline__creditSafeLastRefreshed__c = lastRefreshedDate
            ));
            Accounts.put(a.id, a);
        }

        Set<Id> ids = Accounts.keyset();

        Test.startTest();
            BatchCreditSafeDelta.runBatch();
        Test.stopTest();

        Map<Id, Account> Accounts2 = new Map<Id, Account>();
        for (Account a : [SELECT Id, Name, Last_Month_CreditSafe_Score__c, bottomline__creditSafeLastRefreshed__c, bottomline__creditSafeScore__c,
                            creditSafe_Score_delta__c FROM Account WHERE ID IN :ids])

        {
            System.assertEquals( a.Last_Month_CreditSafe_Score__c, a.bottomline__creditSafeScore__c);
            System.assertEquals( a.CreditSafe_Score_Delta__c != null,  a.CreditSafe_Score_Delta__c != null);
        }       
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
​Raj
Asif Ali MAsif Ali M
Hi Charni,

If the variable is used only in the Test class then it should be defined in test class not in the Actual class.