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
Irish@accIrish@acc 

Copy Lookup field from grandparent whenevr a grand child is inserted

Hi,

 

I'm looking for a trigger which copy the lookup filed from grand parent and paste it on the grandh child is created..

please help me on this...

bvramkumarbvramkumar
With a simple cross object formula field you can access the values of the grand parent record.
Vinit_KumarVinit_Kumar

Irish,

 

Please try below code :- 

 

Following is the relationship among all the objects

 

Contact (Grand Parent) -> Team__c (Parent) -> Team_Player__c (Grand Child) (Opportunity__c is a look up field on Grand Child)

 

trigger updateGrandChild on Team_Player__c (before insert,before update) {

    List<Id> teamIds = new List<Id>();

    for(Team_Player__c tp:trigger.new){
        teamIds.add(tp.Team__c);
    }

   // List<Team__c> teamList = [select Master_Contact__r.Opportunity__c from Team__c where id in:teamIds];
   
   Map<Id,Team__c> teamMap = new Map<Id,Team__c>([select Master_Contact__r.Opportunity__c from Team__c where id in:teamIds]);
   
    
    for(Team_Player__c tp:trigger.new){
    if(!teamMap.IsEmpty()){       
        tp.Opportunity__c = teamMap.get(tp.Team__c).Master_Contact__r.Opportunity__c;
    
    }
    }
    
    
}