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
Shaolin MasterShaolin Master 

Looping through Map adding to a List Returns a Null value

When I run this code I get this error

 

Method does not exist or incorrect signature: void get(Contact) from the type Map<Id,Contact>

 

Map<id,Contact> cMap = new Map<ID, Contact>([SELECT Id, Name FROM Contact LIMIT 5]);

List<Contact> cList;
for(Contact cid : cMap.values()){
    cList.add(cMap.get(cid));
}
AbhinavAbhinav (Salesforce Developers) 
Hi Shaolin,

For your cMap Key Is 'ID' not 'Contact' and its value is Contact.

Please try this code it should work

Map<id,Contact> cMap = new Map<ID, Contact>([SELECT Id, Name FROM Contact LIMIT 5]);
System.debug('----'+cMap);

List<Contact> cList = new List<Contact>();
for(Id cid : cMap.keyset()){
    cList.add(cMap.get(cid));
    System.debug('----'+cList);
}
Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks!