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
sksfdc221sksfdc221 

Trigger to update parent checkbox based on child checkbox

I have a requirement where if the parent (Contact) has more than one child (Lead_Custom__c) and if atleast one child record has Terminated__c checked to true, the Contact's Terminated__c will be updated to true.

Can anyone please suggest the required logic so that I can get this done.

Thanks in advance!
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Please find the sample code below:
/* Please update object names and fields accordingly */

trigger sampleTrigger on Itemsale(after insert){
	set<Id> productIdsToUpdt = new set<Id>();
	for(Itemsale oItem:Trigger.New){
		if(oItem.new){
			productIdsToUpdt.add(oItem.productId__c);  //assuming productId__c is field name for relationship
		}
	}
	
	if(productIdsToUpdt.size()>0){
		list<Product__c> productsToUpdt = new list<Product__c>([SELECT id FROM Product__c WHERE ID IN :productIdsToUpdt]);
		update productsToUpdt
	}	
}

Also,refer the below thread for an another example which might help you.

https://salesforce.stackexchange.com/questions/40701/update-checkbox-on-parent-when-any-of-its-child-records-are-updated

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri