1
votes

I am trying to return fields from all "Billing" contact role Ids, that an Opportunity record may have.

I wish to end up with a map in my opportunity trigger that has opportunity Id and the list of associated contact role Ids (i.e. Map>)

I can create the map keys from looping trigger.new but cannot seem to find a way to insert the list of contact role Ids from my SOQL query.

List<OpportunityContactRole> contactRoleList 
 = new List<OpportunityContactRole>([Select Id 
                                     From OpportunityContactRole 
                                     Where  Role = 'Billing' 
                                       And OpportunityId in :listOfTriggerOppIds
                                    ]);

I can put the rest of the code in if required but seeing as it doesn't actually work, I thought it might confuse things.

1

1 Answers

0
votes

Not sure if I understood your question, but do you mean this :?

Map<id,list<id>> mapOptyBillings = new Map<id,list<id>>(); //map of opportunityID, list<opportunityContactRole IDs

List<OpportunityContactRole> contactRoleList 
 = new List<OpportunityContactRole>([Select Id, OpportunityId
                                     From OpportunityContactRole 
                                     Where  Role = 'Billing' 
                                       And OpportunityId in :listOfTriggerOppIds
                                    ]);
//also query OpportunityId so that you can match

//iterate your result
for(OpportunityContactRole optyCR:contactRoleLis){ 
   //see if our map already has a list for the opportunity of this opportunityContactrole, 
   //if this is not the case, add it with a blank list
   if(! mapOptyBillings.containsKey(optyCR.OpportunityId)mapOptyBillings.put(optyCR.OpportunityId, new List<id>();
   mapOptyBillings.put(optyCR.OpportunityId,optyCR.id);
}