trigger TriggerOpportunityBudget on Opportunity(after insert, after update) {
Map < Id, String > mapAcctIdOppList = new Map < Id, String > ();
set < Id > AccId = new set < Id > ();
for (Opportunity Opp: (List < Opportunity > ) trigger.new) {
AccId.add(Opp.AccountId);
}
List < Opportunity > listOpp = [SELECT Budget__c, AccountId
FROM Opportunity where AccountId in : AccId];
for (Opportunity opp2: listOpp) {
if (String.isNotBlank(opp2.AccountId)) {
if (!mapAcctIdOppList.containsKey(opp2.AccountId)) {
mapAcctIdOppList.put(opp2.AccountId, opp2.Budget__c);
}
mapAcctIdOppList.get(opp2.AccountId).add(opp2.Budget__c);
}
}
system.debug(mapAcctIdOppList);
}
1 Answer
So there is your map is wrong defined I guess that should be
map<Id, List<String>> = new map<Id, List<String>>();
And in you code when you adding into this map
if(!mapAcctIdOppList.containsKey(opp2.AccountId)) {
mapAcctIdOppList.put(opp2.AccountId, new list<String>{opp2.Budget__c });
}
else{
mapAcctIdOppList.get(opp2.AccountId).add(opp2.Budget__c );
}
This way it should work..
Map<Id, String>
and you are trying to add like thismapAcctIdOppList.get(opp2.AccountId).add(opp2.Budget__c);
this is wrong. I guess your map will be likemap<Id, List<String>>
?mapAcctIdOppList.put(opp2.AccountId, new List<String>{opp2.Budget__c} );