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
Girbson Bijou 7Girbson Bijou 7 

Trigger to copy field from parent to child

HI Developers,
I need A trigger  to copy the value of a parent field to a child field. My parent object is Delivery__c, the child object is ItemDestributed__c
The parent child called Status__c and the child field called DelStatus__c. I need that these 2 field have same values all the time.
Rahul.MishraRahul.Mishra
Hi, Please find the sample code of trigger which updates the value on childs when parent is updated:

trigger updateChilds on Delivery__c (before update) {
    
    Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<ItemDestributed__c> lstItemDestriBute = new List<ItemDestributed__c>();
    
    for(Delivery__c del : Trigger.new) {
        if(del.Status__c != trigger.oldMap.get(del.Id).Status__c) {
            
        }
    }
    
    for(ItemDestributed__c itemD : [Select Id, DelStatus__c, Delivery__c From ItemDestributed__c where Delivery__c =:mapOfDeliveryRecords.keySet()]) {
        itemD.DelStatus__c = mapOfDeliveryRecords.get(itemD.Delivery__c).Status__c;
        lstItemDestriBute.add(itemD);
    }
    
    update itemD;

}

Please let me know if this helps for you.

 
Girbson Bijou 7Girbson Bijou 7
Rahul, I made a litle modification in the code see my comment below
 
//copy / update the Del_Status__c of a child object(Item_Distributed__c) with the Delivery_status__c of the Parent Delivery__c

trigger UpdateStatusDeliveryChild on Delivery__c (after insert, after update) {
Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<Item_Distributed__c> lstItemDestriBute = new List<Item_Distributed__c>();
    
    for(Delivery__c del : Trigger.new) {
        if(del.Delivery_status__c != trigger.oldMap.get(del.Id).Delivery_status__c) {
            
        }
    }
    
    for(Item_Distributed__c itemD : [Select Id, Del_Status__c, Delivery__c From Item_Distributed__c where Delivery__c =:mapOfDeliveryRecords.keySet()]) 
    {
        itemD.Del_Status__c = mapOfDeliveryRecords.get(itemD.Delivery__c).Delivery_status__c;
        lstItemDestriBute.add(itemD);
    } // an error message is: Variable Does not existe: itemD
    update itemD;
}

 
Rahul.MishraRahul.Mishra
Nice, did it help you ?, if so, please mark anser as best and close the question.
RKSalesforceRKSalesforce
Hi Girbson,

You are right. Your trigger should be ob after insert and after update. In your code null checks are important those you were missing. Because incase of after insert your Delivery may or May not have associated Distributed items.
Please  find below code for the same. Hope this will work.
//copy / update the Del_Status__c of a child object(Item_Distributed__c) with the Delivery_status__c of the Parent Delivery__c

trigger UpdateStatusDeliveryChild on Delivery__c (after insert, after update) {
Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<Item_Distributed__c> lstItemDestriBute = new List<Item_Distributed__c>();
    
    for(Delivery__c del : Trigger.new) {
        if(del.Delivery_status__c != trigger.oldMap.get(del.Id).Delivery_status__c) {
            mapOfDeliveryRecords.put(del.Id, del);
        }
    }
    if(!mapOfDeliveryRecords.isEmpty()){
		for(Item_Distributed__c itemD : [Select Id, Del_Status__c, Delivery__c From Item_Distributed__c where Delivery__c =:mapOfDeliveryRecords.keySet()]) 
		{
			itemD.Del_Status__c = mapOfDeliveryRecords.get(itemD.Delivery__c).Delivery_status__c;
			lstItemDestriBute.add(itemD);
		} // an error message is: Variable Does not existe: itemD
		
		update itemD;
    }	
}

Please mark as best answer if helped.

Regards,
Ramakant 
Girbson Bijou 7Girbson Bijou 7
HI Ramakant,  I get an error message on the line 19. the message is: Variable does not exist : ItemD
RKSalesforceRKSalesforce
PLease use blow  code:
//copy / update the Del_Status__c of a child object(Item_Distributed__c) with the Delivery_status__c of the Parent Delivery__c

trigger UpdateStatusDeliveryChild on Delivery__c (after insert, after update) {
Map<Id, Delivery__c> mapOfDeliveryRecords = new Map<Id, Delivery__c>();
    List<Item_Distributed__c> lstItemDestriBute = new List<Item_Distributed__c>();
    
    for(Delivery__c del : Trigger.new) {
        if(del.Delivery_status__c != trigger.oldMap.get(del.Id).Delivery_status__c) {
            mapOfDeliveryRecords.put(del.Id, del);
        }
    }
    if(!mapOfDeliveryRecords.isEmpty()){
		for(Item_Distributed__c itemD : [Select Id, Del_Status__c, Delivery__c From Item_Distributed__c where Delivery__c =:mapOfDeliveryRecords.keySet()]) 
		{
			itemD.Del_Status__c = mapOfDeliveryRecords.get(itemD.Delivery__c).Delivery_status__c;
			lstItemDestriBute.add(itemD);
		} // an error message is: Variable Does not existe: itemD
		
		update lstItemDestriBute;
    }	
}
Mark as best answer if helped.

Regards,
Ramakant​