0

I have an existing method where I am looping through OpportunityTeamMember records to get the names and am putting it into a set so I get the unique values, this is working fine. However, I need to adjust the logic to order the names for a join later on. I know that Sets are not ordered and know I need to convert to a list and then sort the list. I can't order by in the initial query because I have to do another method to get the unique picklist values and I don't want to make two separate queries.

I have this code thus far:

public static Map<Id, List<String>> createNameMap(List<OpportunityTeamMember> teamMembers)
{
    Map<Id,Set<String>> oppyMap = New Map<Id,Set<String>>();
    Map<Id,List<String>> oppyList = New Map<Id,List<String>>();

    for(OpportunityTeamMember otm : teamMembers)
    {
        String n = otm.User.FirstName + ' ' + otm.User.LastName;

        if(!oppyMap.containsKey(otm.OpportunityId))
        {
            oppyMap.put(otm.OpportunityId, new Set<String>{n});   
        } else 
        {
            oppyMap.get(otm.OpportunityId).add(n);
        }
    }

    for(Id oppy : oppyMap.keySet())
    {
        List<String> l = new List<String>(XXXXXXXX);
        l.sort();
        oppyList.put(oppy, l);
    }

    return oppyList;

I'm stuck in my second for loop. I don't know how to get the reference of the set from the map. I never give it a variable in the first for loop and I try things like oppy.values() and it doesn't compile with the error:

Method does not exist or incorrect signature: void values() from the type Id

How can I effectively reference the set in the first map to make it a list, sort and then put it into the new map?

2 Answers 2

2

If your goal is simply to get a map collection of <ID, sorted list (with unique values)>, then instead of using multiple collections, you can write a compact code (see code snippet below) using a single map collection variable. The idea is to use contains method of List class. You may also use indexOf method.

Map<Id,List<String>> oppyMap = New Map<Id,List<String>>();
for(OpportunityTeamMember otm : teamMembers)
{
    String n = otm.User.FirstName + ' ' + otm.User.LastName;
    
    if(!oppyMap.containsKey(otm.OpportunityId)){
        oppyMap.put(otm.OpportunityId, new List<String>{n});   
    } 
    else{
        oppyMap.get(otm.OpportunityId).add(oppyMap.get(otm.OpportunityId).contains(n) ? null: n);
    }
}

for(List<String> opptyList : oppyMap.values())
{
    opptyList.sort();
}
1

Well, map.values() return all the values in the map irrespective of the key. Instead, you need values from a particular key.

To get values from the map, you can simply put the set of string to a new List sort it, convert it back to set of String & put it back to the map.

    for(Id oppId : oppyMap.keySet()){
            List<String> valuesList = new List<String>();
          valuesList.addAll(oppMap.get(oppId));
valuesList.sort();
oppyList.put(oppId,valuesList);
        }

working logic:

for(Id oppId : oppyMap.keySet())
{
    List<String> valuesList = new List<String>();
    valuesList.addAll(oppyMap.get(oppId));
    valuesList.sort();
    oppyList.put(oppId, valuesList);
}
3
  • Thanks, I've updated it ! Thought OP needs it back to a set. Oct 23, 2020 at 15:53
  • oppyList is declared with type Map<Id, List<String>>>, so sortedSet is not needed. This code oppyList.put(oppId,valuesList.addAll(opppMap.get(oppId)).sort()); should be sufficient
    – arut
    Oct 23, 2020 at 15:53
  • Thanks, but I get the error: Method does not exist or incorrect signature: void sort() from the type void I got around this by making it multiple lines. Oct 23, 2020 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .