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
Zoom_VZoom_V 

Trigger to update child with parent values

I have this trigger which will update a child record with values from the parent.

 

For some reason I'm getting a "variable does not exist : trigger" error at the line which I underlined : 

 

trigger ChildTriggerToUpdateChildWithParentValues on Grandchild__c (before insert) {
  

   // Get a list of all Parent Ids
   Set<Id> parentIds = new Set<Id>();
    for (child__c chld : trigger.new) {
      parentIds.add(chld.MyParent__c);
   }

   // Get a map of all Parents with Grandparent field information
Map<Id, Parent__c> parentsById = new Map<Id, Parent__c>();
parentsById.putAll([SELECT Id, MyField2__c FROM Parent__c WHERE Id IN :parentIds]);

 for(child__c chld : trigger.new()){ 
      chld.MyField__c=parentsById.get(chld.MyParent__c).MyField2__c;
      //chld.chldfield2__c=parentsById.get(chld.MyParent__c).MyParentfield2__c;
   }

}

 

Any idea what is causing this ? 

 

Thank you.

firechimpfirechimp

Hi Zoom,

Your trigger is on the 'Grandchild__c' object and you are trying to loop round a list of 'Child__c' from the new list.