1

I have 3 custom objects.

Expense__c (Parent)

ExpenseChild__c (Child)

ExpenseGChild__c (Grand Child)

All the relationships between a parent and its immediate child is M-D.

I have built the following trigger that will perform an update in Expense__c (Parent) when the grand children records (ExpenseGChild__c) are updated.

The following code works but I am not sure whether what I have written is in line with the best practices.

Can someone take a look and let me know if the code can be improved further ?

Trigger on Grand Child(ExpenseGChild__c)

trigger GChildTrigger on ExpenseGChild__c (before insert,before update) {

    if(Trigger.isUpdate)
    {
    List<ExpenseGChild__c> egc = Trigger.new;
    List<ExpenseChild__c> ec = new List<ExpenseChild__c>();
    List<Expense__c> e = new List<Expense__c>();
    Set<Id> ExpenseChild_Temp = new Set<Id>();

    for(ExpenseGChild__c egci : egc)
    {
       ec.add(new ExpenseChild__c(Id = egci.ExpenseChild__c,TriggerField__c = egci.TriggerField__c));
       ExpenseChild_Temp.add(egci.ExpenseChild__c);
    }  

    update ec;

    List<ExpenseChild__c> Temp_List = [SELECT ID,Expense__c,Expense__r.Type__c,TriggerField__c from ExpenseChild__c WHERE ID IN :ExpenseChild_Temp];


    for(ExpenseChild__c eci : Temp_List)
    {
        e.add(new Expense__c(Id = eci.Expense__c,Type__c = eci.TriggerField__c));
        System.debug('Id : '+eci.Expense__c+' Triggerfield : '+eci.TriggerField__c);
    }
    update e;
    }
2

If possible you must use formula fields to achieve this. If that is not possible for some reason, here are is the trigger which you should use.

trigger GChildTrigger on ExpenseGChild__c (before insert,before update) {

    if(Trigger.isUpdate)// Do you really want to achieve this only on update? & not on insert? May be change this to - if(Trigger.isInsert || Trigger.isUpdate)
    {
    //List<ExpenseGChild__c> egc = Trigger.new; //variable is waste of memory 
    List<ExpenseChild__c> ec = new List<ExpenseChild__c>();
    //List<Expense__c> e = new List<Expense__c>();
    //Set<Id> ExpenseChild_Temp = new Set<Id>();

    for(ExpenseGChild__c egci : Trigger.New)//Used Trigger.New in place of  egc
    {
       ec.add(new ExpenseChild__c(Id = egci.ExpenseChild__c,TriggerField__c = egci.TriggerField__c));
       //ExpenseChild_Temp.add(egci.ExpenseChild__c);
    }  

    update ec;


    //This code should be in Trigger for ExpenseChild__c
    /*
    List<ExpenseChild__c> Temp_List = [SELECT ID,Expense__c,Expense__r.Type__c,TriggerField__c from ExpenseChild__c WHERE ID IN :ExpenseChild_Temp];


    for(ExpenseChild__c eci : Temp_List)
    {
        e.add(new Expense__c(Id = eci.Expense__c,Type__c = eci.TriggerField__c));
        System.debug('Id : '+eci.Expense__c+' Triggerfield : '+eci.TriggerField__c);
    }
    update e;

    */
 }

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.