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
Michael Clarke 36Michael Clarke 36 

Apex and Test Class don't behave same as Anonymous Window

Hi,
I am a newbie to development and I am trying to create an Apex class that recreates activity of anonymous window code.

Intent is to delete contents of Account_Manager_Budget__c for the current month and repopulate it with data from the Account_Budget__c for the current month.
 
public class PopulateAccountManagerBudget {
@InvocableMethod
    public static void PopulatingBudgets(){
//set current month
	    Integer BudgetYear = System.today().year();
	    Integer BudgetMonth = System.today().month();
		system.debug(BudgetMonth + '/' + BudgetYear);
//Delete all Account Manager Budget records for current month (including forecasts)
		List<Account_Manager_Budget__c> AccMgrBudget = [SELECT Id FROM Account_Manager_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth];
		system.debug(AccMgrBudget.size() + ' records deleted.');
		delete AccMgrBudget;
//Retrieve all current Month Account budgets and sum to Account Manager level
		AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE  Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c];
		system.debug('groupedBudget = ' + groupedBudget.size() + ' results');
//Set up List to insert budget into Account Manager Budget object
		List<Account_Manager_Budget__c> NewAccMgrBudget = new List<Account_Manager_Budget__c>();
		String AM_Id = '';
		String AM_Budget = '';
//Loop through budgets and assign to List
		for(AggregateResult ar:groupedBudget){
        	AM_Id = String.valueof(ar.get('Account_Manager__c'));
    	    AM_Budget = string.valueof(ar.get('Budget'));
    		System.debug('Account Manager = ' + String.valueof(ar.get('Account_Manager__c')) + ', BudgetYear = ' + BudgetYear + ', BudgetMonth = ' + BudgetMonth + ', Budget = ' + string.valueof(ar.get('Budget')));
//		    Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=String.valueof(ar.get('Account_Manager__c')), Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(string.valueof(ar.get('Budget'))));
            Account_Manager_Budget__c amb = new Account_Manager_Budget__c(Account_Manager__c=AM_Id, Year__c=BudgetYear, Month__c=BudgetMonth, Budget__c=decimal.valueof(AM_Budget));
            NewAccMgrBudget.add(amb);
		}
//insert list into Account Manager Budget object for current month, with Forecast = 0
		if(NewAccMgrBudget.size()>0){
		insert NewAccMgrBudget;
		}
    }
}

The aggregate result returns 9 records in Anonymous window but zero records when testing the Apex Class.

Test Class:
@isTest
private class PopulateAccountManagerBudget_test{

    @isTest static void MyTestClass(){
		PopulateAccountManagerBudget.PopulatingBudgets();
        //system.debug('RecourdCount=' + recordCount);
        Integer recordCount = 0;


	    Integer BudgetYear = System.today().year();
	    Integer BudgetMonth = System.today().month();
		system.debug(BudgetMonth + '/' + BudgetYear);

        
        //        AggregateResult[] countRecords = [SELECT COUNT(Id)recordCount FROM Account_Manager_Budget__c WHERE Budget_Date__c = :dateBudgetDate];
		AggregateResult[] groupedBudget = [SELECT Account_Manager__c, SUM(Budget__c)Budget FROM Account_Budget__c WHERE Year__c = :BudgetYear AND Month__c = :BudgetMonth GROUP BY Account_Manager__c];
		for(AggregateResult ar:groupedBudget){
            recordCount = recordCount+1;
        }
        system.assertEquals(9, recordCount);
    }
}

System.assertEquals shows recountCount = 0 but I know there are Account Budget records for 9 Account Managers that should be aggregated (and are in Anonymous window).

I would appreciate your expert eyes on this.
Michael Clarke 36Michael Clarke 36
Calling the Apex Class from the anonymous window also works, only the Test Class fails.
PopulateAccountManagerBudget.PopulatingBudgets();

 
Michael Clarke 36Michael Clarke 36
The FOR Loop is bypassed completely (zero records)
for(AggregateResult ar:groupedBudget){

 
Michael Clarke 36Michael Clarke 36
I have found the apex behaves as expected in production but not in the test. Is it something to do with the Test Class?