0

I'm working on the following method for class. It maps a list of users that is retrieved via a SOQL query to their assigned languages (Champion_for_Language_c).

Whenever I try to save this method I get the following error: Method does not exist or incorrect signature: void add(User) from the type Set (130:42)

Any help would be greatly appreciated.

private static Map<String, Set<Id>> mapUsersByLanguage(List<User> users){
        Map<String, Set<Id>> retVal = new Map <String, Set<Id>>();

        for(User user : users){

            String languageChampion = user.Champion_for_Language__c;

            if(retVal.containsKey(languageChampion)){

                retVal.put(languageChampion, new Set<Id>());

            }

            retVal.get(languageChampion).add(user);
        }
        return retVal;
1
  • 4
    Your Set is a collection of Id whereas you're trying to add a User object to it. Take a look at Apex Collections trailhead
    – identigral
    Sep 6, 2020 at 16:20

1 Answer 1

0

You need to tweak your code like below for(User user : users){

        String languageChampion = user.Champion_for_Language__c;

        if(retVal.containsKey(languageChampion)){
            set<Id> temp=  retVal.get(languageChampion);
            temp.add(user.Id);
            retVal.put(languageChampion, temp);
        }else{
            retVal.put(languageChampion, new Set<Id>{user.Id});
        }
      }
        return retVal;
1
  • you can collapse the three lines for the if's true condition to retval.get(languageChampion).add(user.id);
    – cropredy
    Sep 6, 2020 at 19:16

You must log in to answer this question.

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