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
shraddha kawaneshraddha kawane 

Trigger to update parent checkbox if child record has atleast 1 checkbox ticked.

I have the following requirement :

object : Product
fields: 
1. New (checkbox)
2. Old ( checkbox)
3. Sold(checkbox)
4. Out of stock(checkbox)
5.Item (lookup to Itemsale)

object: Itemsale

1. New (checkbox)
2. Old ( checkbox)
3. Sold(checkbox)
4. Out of stock(checkbox)

how to write the trigger so that atleast one record of itemsale has "New" checked then it will update parent field "New" as checked?

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi Shraddha,

This might help you
/* 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
	}	
}

 

Thanks,
N.J
 

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
You might have to update all expected fields in if condition you you want to update multiple fields on parent.