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
Priyesh Misquith 12Priyesh Misquith 12 

Please explain this map logic

List<Contact> lstContact = [select id,firstName,LastName,accountId from contact where accountid in :stAccId ];

Map<String,List<Contact>> mapAccountWiseContact = new Map<String,List<Contact>>();

For(Contact cont : lstContact)
{
	if(mapAccountWiseContact.containsKey(cont.accountId))
	{
		List<Contact> lstCont = mapAccountWiseContact.get(cont.accountId);
		lstCont.add(cont);
	}
	else
	{
		List<Contact> lstCont = new List<Contact>();
		lstCont.add(cont);
		mapAccountWiseContact.put(cont.accountId,lstCont);
	}
}

What does if and else part does.
Best Answer chosen by Priyesh Misquith 12
Ajay K DubediAjay K Dubedi
Hi Priyesh,

I have gone through the script. I have explained the code in the comments. 
I have also added a line in the script as it was needed.
 
List<Contact> lstContact = [select id,firstName,LastName,accountId from contact where accountid in :stAccId ];

Map<String,List<Contact>> mapAccountWiseContact = new Map<String,List<Contact>>();

//All contacts are iterated
For(Contact cont : lstContact)
{
        //Check if map contains the accountId means it is already filled. 
        //This block will not run when accountId comes for the first time
    if(mapAccountWiseContact.containsKey(cont.accountId))
    {
                //Here all the previous values of the map are added to the new list
        List<Contact> lstCont = mapAccountWiseContact.get(cont.accountId);
                //Here current contactis added to the list
        lstCont.add(cont);
                //I added this line.
                //This new line replaces the map's current index with updated value.
                mapAccountWiseContact.put(cont.accountId,lstCont);
    }
        //This block runs when accountId comes for the first time 
        //So it will only run once per unique accountId
    else
    {
                //create new list for first time
        List<Contact> lstCont = new List<Contact>();
                //Here current contactis added to the list
        lstCont.add(cont);
                //This line adds new id and value pair in the map for the first time
        mapAccountWiseContact.put(cont.accountId,lstCont);
    }
}




I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi priyesh,
Here if is checking that the accountid of contact exists in the map or not.
If the map contains accountId of the contact,it will add the current contact in the loop to the corresponding list of contacts in the map. else if the map doesn't contain account id of contact it will put the accountid as well as contact list into the map
List<Contact> lstContact = [select id,firstName,LastName,accountId from contact where accountid in :idlist ];

Map<String,List<Contact>> mapAccountWiseContact = new Map<String,List<Contact>>();

For(Contact cont : lstContact)
{
	if(mapAccountWiseContact.containsKey(cont.accountId))
	{
                /*if accountId exists in the map 
                fetching list<contact> related to accountId and add cont to this list*/

                List<Contact> lstCont = mapAccountWiseContact.get(cont.accountId);
		lstCont.add(cont);
	}
	else
	{
               /*if accountId is not in the map then create a new list<contact> by adding  
               adding current contact to the list and add then accountid and this list<contact> 
               to the map */

		List<Contact> lstCont = new List<Contact>();
		lstCont.add(cont);
		mapAccountWiseContact.put(cont.accountId,lstCont);
	}
}

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Ajay K DubediAjay K Dubedi
Hi Priyesh,

I have gone through the script. I have explained the code in the comments. 
I have also added a line in the script as it was needed.
 
List<Contact> lstContact = [select id,firstName,LastName,accountId from contact where accountid in :stAccId ];

Map<String,List<Contact>> mapAccountWiseContact = new Map<String,List<Contact>>();

//All contacts are iterated
For(Contact cont : lstContact)
{
        //Check if map contains the accountId means it is already filled. 
        //This block will not run when accountId comes for the first time
    if(mapAccountWiseContact.containsKey(cont.accountId))
    {
                //Here all the previous values of the map are added to the new list
        List<Contact> lstCont = mapAccountWiseContact.get(cont.accountId);
                //Here current contactis added to the list
        lstCont.add(cont);
                //I added this line.
                //This new line replaces the map's current index with updated value.
                mapAccountWiseContact.put(cont.accountId,lstCont);
    }
        //This block runs when accountId comes for the first time 
        //So it will only run once per unique accountId
    else
    {
                //create new list for first time
        List<Contact> lstCont = new List<Contact>();
                //Here current contactis added to the list
        lstCont.add(cont);
                //This line adds new id and value pair in the map for the first time
        mapAccountWiseContact.put(cont.accountId,lstCont);
    }
}




I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer