0
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);
}
3
  • your map is Map<Id, String> and you are trying to add like this mapAcctIdOppList.get(opp2.AccountId).add(opp2.Budget__c); this is wrong. I guess your map will be like map<Id, List<String>> ?
    – Ratan Paul
    Jun 6 '16 at 7:05
  • Error: Compile Error: Incompatible key type opportunity for Map<Id,List<String>> for this line mapAcctIdOppList.put(opp2.AccountId,opp2.Budget__c );
    – SDSM
    Jun 6 '16 at 7:16
  • ahh this line should be mapAcctIdOppList.put(opp2.AccountId, new List<String>{opp2.Budget__c} );
    – Ratan Paul
    Jun 6 '16 at 7:20
1

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..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.