I have a very simple trigger that for some reason doesn't want to work. Using a before insert trigger, I want to populate a lookup field on a grandchild object with the grandparent objects OwnerId. When I try it, nothing happens. If you know why, can you please tell me how to fix it? Thank you.

trigger someName on grandChild__c (before insert){

for(grandChild__c a: trigger.new)

if(a.Approver__c == null)
    a.Approver__c == a.Parent__r.Grandparent__r.OwnerId;

You will have to query for the values of a.Parent__r.Grandparent__r.OwnerIdas they are not available in the trigger context.

So something like

Map<ID,parent__c> gcMap = new Map<ID,parent__c>();

for(grandchild__c gc : trigger.new)

gcMap = New Map<ID,Parent__c>([Select Grandparent__r.OwnerId from parent__c where ID IN :gcMap.keySet()]);

for(grandchild__c gc : trigger.new){
   if(gcMap.containsKey(gc.parent__c) && !gcMap.get(gc.parent__c).Grandparent__r.isEmpty())
       a.Approver__c = gcMap.get(gc.parent__c).Grandparent__r.OwnerID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.