+ Start a Discussion
MattreyMattrey 

Error Method does not exist or incorrect signature: [MAP<Id,User>].add(Id, SOBJECT:User)

I'm trying to create a map from a query list I created so I can access it by Id - here's the relevant code:

 

      List<User>userResults= new List<User>();
      set<Id>userIds = new Set<Id>();
      Map<Id,User>userData = new Map<Id,User>();

 

//code that fills user ids

 

    //get the relevant user data      
    userResults=[SELECT Id,MyField1__c,MyField2__c FROM User WHERE Id in: userIds];


    //put it into a map for easier access
    for (User u : userResults){
        userData.add(u.Id,u); //here's where the error is
    }

My List is of type User, the for loop u value is of type User and my map is Id, User so why doesn't it work?

Best Answer chosen by Admin (Salesforce Developers) 
middha.vikram@gmail.commiddha.vikram@gmail.com

Replace userData.add(u.Id,u); with userData.put(u.Id,u);

 

 

All Answers

middha.vikram@gmail.commiddha.vikram@gmail.com

Replace userData.add(u.Id,u); with userData.put(u.Id,u);

 

 

This was selected as the best answer
garybgaryb

As Middha said. Also, FYI, you can do the following:

 

 

Map<Id,User>userData = new Map<Id,User>([SELECT Id,MyField1__c,MyField2__c FROM User WHERE Id in :userIds]);

 

 

MattreyMattrey

yeah I guess it would help if I used the right method name! Too much time with sets and lists I guess...

 

Thanks for that quick response too

MattreyMattrey

Garyb - thanks for that tip. I didn't think it would be smart enough to do something like that