0

I wanted to know how to get the second element of my oppTeamMemberWrapper map knowing that its ID exists in several times in the wrapper map (the ID being the ID of the parent opportunity)

2

There seems to be a misunderstanding about how Maps work here. A Map relates a single key to a single value, and its purpose is to allow you to determine the value associated with any key without iterating over the Map. Your code has several problems related to types, but it's not clear enough what you're trying to do to fully correct them.

Map<ID, OpportunityTeamMember> wrapper = new Map<ID, OpportunityTeamMember>();

This Map relates a single Id as key to a single OpportunityTeamMember as value. If you want to store more than one OpportunityTeamMember, you need a Map<Id, List<OpportunityTeamMember>>.

 for (ID oppID : idOpps){
     teamMerbers = new List<OpportunityTeamMember>();

You aren't going to be able to place teamMerbers in your Map for the reason noted above.

     for (ID oppTeamMemberWrapper : wrapper.keySet()){
         if(oppTeamMemberWrapper == oppID){

This is an example of a combinatoric search, where you iterate over two collections in nested for loops to find a match by comparing each pair of items for equality. It is incredibly inefficient and wasteful, and is exactly what the Map is supposed to fix.

Instead of iterating, you can access the value associated with oppId by doing wrapper.get(oppId).

             teamMerbers.add(//Add second element of oppTeamMemberWrapper );

It's not clear what this is supposed to do. oppTeamMemberWrapper is an Id, which does not have a second element. As mentioned above, if you're trying to collect more than one OpportunityTeamMember per Opportunity, you need a different Map type.

          }
     }
 }
1
  • @cpt_3n0x You could try posting over on the Code Review Stack Exchange for a general review. SFSE is more oriented towards specifics, so if you can pull out well-defined issues you're struggling with and give a lot of detail, you should open further questions to get assistance. – David Reed Nov 2 '18 at 13:46
0

This line will give you the second attribute of map that is value by passing key. Just replace it.

 teamMerbers.add(wrapper.get(oppTeamMemberWrapper));

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.