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
Salesforce BeginnerSalesforce Beginner 

To create a map of parent and child.

Hello All,

I have a contact object and custom__c object. Where in Custom__c has a lookup relation to contact. My requirement is, for every contact record, have to loop through all the child records. For this I need a map<contact_Id, List<custom__c>>. 

I am trying different ways to do it with no luck. Any thoughts on this is appreciated.

Once I have a map, I am planning to do the following:
for (ID conid:  map.keyset())
{
      for(custom__c cust : map.get(conid))
      {
            if(cust.status==True)
            {
                   // update few fields on contact
            }
      }
}
Best Answer chosen by Salesforce Beginner
Amit Chaudhary 8Amit Chaudhary 8
I have created the code base on account and contact. you can convert it according to your objects
Please try below code :-
Option 1:- 
Set<String> stAccId = new Set<String>();
For(Account acc : Trigger.New)
{
	stAccId.add(acc.id);
}

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

Option 2:- 
 
Set<String> stAccId = new Set<String>();
For(Account acc : Trigger.New)
{
	stAccId.add(acc.id);
}

List<Account> listAccount = [select id,Name ,(select id,firstName,lastName from contacts) from account where in :stAccId];

For(Account acc : listAccount)
{
	List<Contact> lstContact = acc.contacts;
	for(Contact cont :lstContact)
	{
		System.debug(cont.FirstName);
		// you can access all field here
	}
}


Please mark this as solution if this will help you. So that if any one has same issue this post can help
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
I have created the code base on account and contact. you can convert it according to your objects
Please try below code :-
Option 1:- 
Set<String> stAccId = new Set<String>();
For(Account acc : Trigger.New)
{
	stAccId.add(acc.id);
}

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

Option 2:- 
 
Set<String> stAccId = new Set<String>();
For(Account acc : Trigger.New)
{
	stAccId.add(acc.id);
}

List<Account> listAccount = [select id,Name ,(select id,firstName,lastName from contacts) from account where in :stAccId];

For(Account acc : listAccount)
{
	List<Contact> lstContact = acc.contacts;
	for(Contact cont :lstContact)
	{
		System.debug(cont.FirstName);
		// you can access all field here
	}
}


Please mark this as solution if this will help you. So that if any one has same issue this post can help
 
This was selected as the best answer
Salesforce BeginnerSalesforce Beginner
Thanks for the help Amith. The above solution saved my day.
NavJyoti PandeyNavJyoti Pandey
it's interesting....
Sergio Flores 19Sergio Flores 19
I voted for the best answer but accidentally disliked it, how do you undo the dislike?
Rahul Bansal 22Rahul Bansal 22
public map<Account,List<Contact>> accountContactMap = new map<Account,List<Contact>>();
List<Account> lstAccount = [SELECT Id, name, (SELECT Id, Name FROM Contacts) FROM Account]; for(Account acc : lstAccount) { this.accountContactMap.put(acc, acc.Contacts);
}
System.debug(this.accountContactMap);