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
Abhishek chauhan 20Abhishek chauhan 20 

query to put in map " all child record related to each different parent " Total_Child_Count " & " Id " from parent filed

Best Answer chosen by Abhishek chauhan 20
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Did you try like below approach?
 
List<Contact> contactList=[select id,accountid from Contact where accountid!=null];
        map<Id,List<Contact>> contactMap=new map<Id,List<Contact>>();
        for(Contact con:contactList){
            if(contactMap.containsKey(con.accountid)){
                contactMap.get(con.AccountId).add(con);
            }else{
                contactMap.put(con.AccountId,new List<Contact>{con});
            }
        }

This will store the Accountid and list of contacts in the map.

I guess this may not be possible with single SOQL query.

Let me know if you face any issues.

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

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Do you mean you want to put all the child record as the values and Parent Id as key in a map?

Thanks,
 
Abhishek chauhan 20Abhishek chauhan 20
yes
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

Did you try like below approach?
 
List<Contact> contactList=[select id,accountid from Contact where accountid!=null];
        map<Id,List<Contact>> contactMap=new map<Id,List<Contact>>();
        for(Contact con:contactList){
            if(contactMap.containsKey(con.accountid)){
                contactMap.get(con.AccountId).add(con);
            }else{
                contactMap.put(con.AccountId,new List<Contact>{con});
            }
        }

This will store the Accountid and list of contacts in the map.

I guess this may not be possible with single SOQL query.

Let me know if you face any issues.

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

Thanks,
This was selected as the best answer
Abhishek chauhan 20Abhishek chauhan 20
Yes, that is correct. Thank you i need some more help hi i want to add two field of child obj an put sum in a parent obj || name of child obj = " Object_2__c " field name = " Amount_Paid__c & Amount_Due__c " result in parent obj name = " Object_2__c " field name = " Sum__c"
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Abhishek,

 if you have other question please post it as new question.

Thanks,