function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

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)

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



All Answers

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



This was selected as the best answer

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]);




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


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