0

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

}
4
  • Welcome to SFSE! Please take a moment to read How to Ask and take the tour. Including a Minimal, Complete and Reproducible example would be helpful.
    – identigral
    Commented Feb 27 at 18:59
  • Welcome to SFSE. Please take a moment to read How to Ask and take the tour. Please edit your post to include the trigger code as well. Commented Feb 27 at 19:19
  • 1
    You need something to call the getter. They don't automatically run. Further, be careful of name shadowing. Declaring a separate variable in the getter won't update the property defined in the class.
    – sfdcfox
    Commented Feb 27 at 20:08
  • 1
    per sfdcfox, this line is using name shadowing Map<Id, List<Product_Forecast__c>> accountProductForecastMap = new Map<Id, List<Product_Forecast__c>>();. Don't redeclare the variable
    – cropredy
    Commented Feb 28 at 0:08

1 Answer 1

0

You fell into a common error in getters (I make 'em myself)

The variable you want to return is declared here

private static Map<Id, List<Product_Forecast__c>> accountProductForecastMap{
    get{
       ...

    } set;
}

but inside the getter, you redeclare it:

Map<Id, List<Product_Forecast__c>> accountProductForecastMap = 
       new Map<Id, List<Product_Forecast__c>>();

so, all of the code from then on is modifying a local variable and NOT the variable defined with the getter

Simply change the errant line to:

accountProductForecastMap = new Map<Id, List<Product_Forecast__c>>();

and you'll get past your issue.

2
  • Thank you! Its working like a charm now (: Commented Mar 4 at 17:39
  • great - community convention is to accept the answer so others who encounter this issue will know there was a solution. Use the check mark by the answer
    – cropredy
    Commented Mar 4 at 18:40

You must log in to answer this question.

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