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
ForceRookieForceRookie 

How to query child from parent?

Hi!

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;
		campList.add(parentCamp);
	}
	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

Hi,

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){
            accIDSet.add(c.accountId);
        }
    }
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!.