+ Start a Discussion
SandrineSandrine 

Implemtation of trigger to update the checkbox in child based on picklist value of parent

Hello,

I have a picklist__c on account with value1 and Value2
On the object_Custom__c i have check box checkbox1__c

usecase:
On account insert or update
 If picklist__c == value1
then checkbox1__c == true
else if 
picklist__c == value2 
then checkbox1__c == false

usecase 2:
On creation of new record of object_Custom__c  the field checkbox1__c will use same logic of usecase 1

How can implement a trigger for the same ?
thank you for suggestion 
Best Answer chosen by Sandrine
Shivam Yadav 8Shivam Yadav 8

Hi Sandrine,

Here is the Trigger Code for above problem

Trigger UpdateCHild on ParentObject(after insert, after update) {
Map<Id, ParentObject> mapOfParent = new Map<Id, Parentobject>();
for(ParentObject po : trigger.new) {
     mapOfParent.put(po.id, po);
}

if(mapOfParent.size()>0) {
List<ChildObject> updateChild = new List<ChildObject>();
for(ChildObject chl : [Select id,checkbox__c from ChildObject where ParentObject IN : mapOfParent.keyset() ]) {
ParentObject pop = mapOfParent.get(chl.ParentObject);
if(pop.picklist__c == 'value1') {
 chl.checkbox__c  = true;
updateChild.add(chl);
}
if(pop.picklist__c  == 'value2') {
chl.checkbox__c  = false;
updateChild.add(chl);
}
}
update updateChild;
}

}