I wrote a getter/setter variable to populate a map when an opportunity is updated. For some reason the code inside the accountProductForecastMap method is not being ran. I have used this similar getter/setter framework before without issue.
Trigger Handler Code :
public with sharing class OpportunityTriggerHandler extends TriggerHandler {
private static List<Opportunity> newList;
private static List<Opportunity> oldList;
private static Map<Id, Opportunity> newMap;
private static Map<Id, Opportunity> oldMap;
public OpportunityTriggerHandler() {
super('OpportunityTriggerHandler');
newList = (List<Opportunity>) Trigger.new;
oldList = (List<Opportunity>) Trigger.old;
newMap = (Map<Id, Opportunity>) Trigger.newMap;
oldMap = (Map<Id, Opportunity>) Trigger.oldMap;
}
/*************** Getter/Setter Variables ****************/
private static Map<Id, List<Product_Forecast__c>> accountProductForecastMap{
get{
System.debug('In Product Forecast Getter');
if (accountProductForecastMap == null) {
Set<Id> relatedAccountIds = new Set<Id>();
for (Opportunity opp : newList) {
if(opp.AccountId != null) {
relatedAccountIds.add(opp.AccountId);
}
}
List<Product_Forecast__c> productForecastList = [
SELECT Id, Account__r.Id, Forecast_End_Date__c,
Forecast_Start_Date__c, Product__c, Target_Quantity__c
FROM Product_Forecast__c
WHERE Account__r.Id
IN :relatedAccountIds
];
Map<Id, List<Product_Forecast__c>> accountProductForecastMap = new Map<Id, List<Product_Forecast__c>>();
for (Product_Forecast__c productForecast : productForecastList) {
if(accountProductForecastMap.containsKey(productForecast.Account__r.Id)) {
//add product forecast to list of values
accountProductForecastMap.get(productForecast.Account__r.Id).add(productForecast);
} else {
// add account id as key, and product forecast to list of values
accountProductForecastMap.put(productForecast.Account__r.Id, new List<Product_Forecast__c>{productForecast});
}
}
}
System.debug('Product Forecast Map: ' + accountProductForecastMap);
return accountProductForecastMap;
} set;
}
}
Trigger Code :
trigger OpportunityTrigger on Opportunity (before insert,
before update,
before delete,
after insert,
after update,
after delete,
after undelete) {
new OpportunityTriggerHandler().run();
}
Map<Id, List<Product_Forecast__c>> accountProductForecastMap = new Map<Id, List<Product_Forecast__c>>();
. Don't redeclare the variable