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
Nevin O'Regan 3Nevin O'Regan 3 

Trigger to sum a value from child to parent object when object had parent child relationship to itself

Hi guys,

I have a custom object called Payment__c, which has a lookup field to itself. I have a custom field called Replaced_Amount__c which I want to sum up the total amount of related payment records. I think the only way of doing this is through a trigger.

I have seen a lot of answers in the community on how to achieve a similar requirement but I haven't seen one where the object has a lookup to itself. 

Could anyway help me with this?
Best Answer chosen by Nevin O'Regan 3
Nevin O'Regan 3Nevin O'Regan 3
Hi guys,

After further research I found a trigger in the group and made a few adjustments which seem to be working fine. See below:

trigger SumOfReplacedValue on Payment__c (after insert, after update, after delete, after undelete) {
Set<ID> setID = new Set<ID>();
    List<Payment__c> lstPay = new List<Payment__c>();
    
    if(trigger.isinsert || trigger.isundelete){
        for(Payment__c p : trigger.new){
            setID.add(p.Replaced_Payment__c);
        }
    }
    else if(trigger.isDelete){
        for(Payment__c p : trigger.old){
            setID.add(p.Replaced_Payment__c);
        }
    }
    
    else if(trigger.isUpdate){
         for(Payment__c p : trigger.new){
            if(p.Replaced_Payment__c != null){
                if(trigger.oldmap.get(p.id).Replaced_Payment__c != p.Replaced_Payment__c){
                    setID.add(p.Replaced_Payment__c);     
                }
            } 
            setID.add(trigger.oldmap.get(p.id).Replaced_Payment__c);
         }
    }
    if(setid.size() > 0){
        lstPay = [Select id,Replaced_With__c,(Select id,Payment_Amount__c from Replaced__r) from Payment__c where id IN : setID];
    }
    for(Payment__c pay : lstPay){
        Decimal val = 0;
        for(Payment__c rep : pay.Replaced__r){
            
            val += rep.Payment_Amount__c;
            
        }
        pay.Replaced_With__c  = val;
    }
    update lstPay;
}

All Answers

Nevin O'Regan 3Nevin O'Regan 3
Hi guys,

After further research I found a trigger in the group and made a few adjustments which seem to be working fine. See below:

trigger SumOfReplacedValue on Payment__c (after insert, after update, after delete, after undelete) {
Set<ID> setID = new Set<ID>();
    List<Payment__c> lstPay = new List<Payment__c>();
    
    if(trigger.isinsert || trigger.isundelete){
        for(Payment__c p : trigger.new){
            setID.add(p.Replaced_Payment__c);
        }
    }
    else if(trigger.isDelete){
        for(Payment__c p : trigger.old){
            setID.add(p.Replaced_Payment__c);
        }
    }
    
    else if(trigger.isUpdate){
         for(Payment__c p : trigger.new){
            if(p.Replaced_Payment__c != null){
                if(trigger.oldmap.get(p.id).Replaced_Payment__c != p.Replaced_Payment__c){
                    setID.add(p.Replaced_Payment__c);     
                }
            } 
            setID.add(trigger.oldmap.get(p.id).Replaced_Payment__c);
         }
    }
    if(setid.size() > 0){
        lstPay = [Select id,Replaced_With__c,(Select id,Payment_Amount__c from Replaced__r) from Payment__c where id IN : setID];
    }
    for(Payment__c pay : lstPay){
        Decimal val = 0;
        for(Payment__c rep : pay.Replaced__r){
            
            val += rep.Payment_Amount__c;
            
        }
        pay.Replaced_With__c  = val;
    }
    update lstPay;
}
This was selected as the best answer
GauravGargGauravGarg
Hi Nevin, 

I have gone through the trigger and understand the requirement is to calculate the "Total Payment" based on the related object records. 
You can achieve above functionality with Process Builder + "Lightning Flow" combined. Where process builder will help to call Lightning flows based on the Trigger event i.e. update, insert, undelete and Lightning flow will re-calculate all the total sum based on the relative records. 

The second option is the trigger which you already wrote. 

Hope this will help. 

If you need any more help you can directly contact me on skype gaurav62990, email: gauravgarg.nmims@gmail.com

Thanks,
Gaurav Garg
Salesforce Consultant