+ Start a Discussion

How to query child from parent?


Please help me on how to query this. The scenario is:
If any if the child record has true value, then update parent record to true. But if all child record is false, then update parent record to false.
List<Campaign__c> campList = new List<Campaign__c>();
if (!parentIds.isEmpty()) {
	for (Campaign__c parentCamp : [SELECT Id, Do_Not_Notify_Subscriber__c FROM Campaign__c WHERE Id IN :parentIds]) {
		Campaign__c childCamp = childMatterMap.get(parentCamp.Id);
		parentCamp.Do_Not_Notify_Subscriber__c = childCamp.Do_Not_Notify_Subscriber__c;
	if (!campList.isEmpty()) {
		update campList;

Martha VMartha V
what is the problem with the code?  It looks right assuming that:
  • you have a map (childMatterMap) with the children's Campaign__c records, and that the key for the map is the id of the Parent record and not the child's record. 
  • you have a list of id's for all the Parent records (parentIds)
have you debugged and figured where is the problem?
Balagopal GBalagopal G


Hope this is what you are looking for.

Set<String> accIDSet=new Set<String>();
list<contact> conList= // either get the list from trigger or get it as query.
for(contact c: conList){
        if(c.booleanField__c== true && c.AccountId != null){
list<account> accList = [SELECT id, connect_America_Account__c from Account WHERE Id =: accIDSet];
    for(Account a: accList ){
        a.booleanField__c= true;
    update accList ;


Thank you .

Have a nice day!.