+ Start a Discussion
SandrineSandrine 

Trigger to Update child based on value of parent

Hello,

I have a Custom_picklist__c on Parent_Obect__c with value1 and Value2
I have a child object child_Custom__c i have check box on this object Custom_checkbox1__c

Parent_Obect__c
  Custom_picklist__c (value1, Value2)

child_Custom__c
  Custom_checkbox1__c
    
Usecase:
On creation of new record of child_Custom__c  the field Custom_checkbox1__c will be if Custom_picklist__c = Value1

How can implement a trigger for the same ?

thank you for suggestion 
Best Answer chosen by Sandrine
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger UpdateChildBasedOnParent on Child_Custom__c (before insert) {
    
    Map<ID, Parent_Object__c> parent = new Map<ID, Parent_Object__c>();
    Set<Id> listIds = new Set<Id>();
    
    for (Child_Custom__c childObj : Trigger.new) {
        listIds.add(childObj.Parent_Object__c); //API name of parent field in child object
    }
    
    parent = new Map<Id, Parent_Object__c>([SELECT Id, Custom_Picklist__c, (SELECT Id, Custom_Checkbox__c FROM Child_Custom__c) 
                                   FROM Parent_Object__c 
                                   WHERE ID IN :listIds]);
    
    for (Child_Custom__c cont: Trigger.new){
        Parent_Object__c p = parent.get(cont.Parent_Object__c); //Parent_Object__c is an API name of parent field in child object
        if(cont.Parent_Object__c!=null && p.Custom_Picklist__c=='Value1'){
            cont.Custom_Checkbox__c=true;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger UpdateChildBasedOnParent on Child_Custom__c (before insert) {
    
    Map<ID, Parent_Object__c> parent = new Map<ID, Parent_Object__c>();
    Set<Id> listIds = new Set<Id>();
    
    for (Child_Custom__c childObj : Trigger.new) {
        listIds.add(childObj.Parent_Object__c); //API name of parent field in child object
    }
    
    parent = new Map<Id, Parent_Object__c>([SELECT Id, Custom_Picklist__c, (SELECT Id, Custom_Checkbox__c FROM Child_Custom__c) 
                                   FROM Parent_Object__c 
                                   WHERE ID IN :listIds]);
    
    for (Child_Custom__c cont: Trigger.new){
        Parent_Object__c p = parent.get(cont.Parent_Object__c); //Parent_Object__c is an API name of parent field in child object
        if(cont.Parent_Object__c!=null && p.Custom_Picklist__c=='Value1'){
            cont.Custom_Checkbox__c=true;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Rajesh3699Rajesh3699
Hi 


@Khan, just a small correction :-)
Use the above trigger with only Before Insert event and make the listIds as Set instead of list.

Thank You,
Rajesh Adiga P.