I am attempting to calculate the sum MRR of Assets related to an Account and add it to a field on the Opportunity object. The code doesn't show an error but does not return values when new Opportunities are created. This is a Trigger.isBefore class.
Map<Id,List<Opportunity>> qualifiedOpportunities = new Map<Id,List<Opportunity>>();
for(Opportunity opp : opportunities)
{
if(opp.Type == 'Past Due' && Trigger.isInsert)
{
if(! qualifiedOpportunities.containsKey(opp.Location__c) ) qualifiedOpportunities.put(opp.Location__c,new List<Opportunity>());
//ADD TO QUALIFIED OPPS TO SET AMOUNT BEFORE PROCESSING
qualifiedOpportunities.get(opp.Location__c).add(opp);
}
}
for(AggregateResult a : [SELECT SUM(Current_Price__c),Location__c FROM Asset WHERE Location__c IN :qualifiedOpportunities.keySet() AND Status = 'Active' GROUP BY Location__c])
{
for(Opportunity opp : qualifiedOpportunities.get((Id) a.get('Location__c')))
{
opp.Past_Due_MRR__c = (Decimal) a.get('expr0');
}
}
}
Location__c
non-null?Location__c
field is a required field on the opportunity.