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
Neha LambatNeha Lambat 

Query all Contacts in the system and put them in a map. Key of the map will be the ID of the contact and the value will be Contact.

i want answer this question
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Neha,

Can you try the below code. You can add what ever fields you need in query
 
Map<ID, Contact> mapcon = new Map<ID, Contact>([SELECT Id, LastName FROM Contact]);
system.debug('asas'+mapcon);

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Shams TabrezShams Tabrez
Hi Neha,

Please use and execute the below code. You will get all the contact Ids and queried values.
public class AllContacts {    
    
    public static void allContacts(){
        
        List<Contact> lstAllCons = [Select Id,LastName from Contact];
        Map<Id,Contact> mapOfCon = new Map<Id,Contact>(lstAllCons);
        Set<Id> conids = new Set<Id>();
        
        for(Contact con : lstAllCons){
            conids.add(con.Id);
        }
        
        if(!conids.isEmpty()){
            for(Id conId : conids)
                if(mapOfCon.containsKey(conId)){
                    System.debug(+mapOfCon.get(conId));
                }   
        }
        
    }
}

containsKey(key): Returns true if the map contains a mapping for the specified key.
get(key):  When we give a key it will return values.

Thanks & Regards,
Shams Tabrez