+ Start a Discussion
Nevin O'Regan 3Nevin O'Regan 3 

Help With A Roll Up Trigger

Hi guys,

I'm looking for some help with a trigger that I have built on a custom object which has a lookup field to itself. 

I object Payment__c has a custom picklist field Payment_Status__c with a value of 'Cleared'. I only want the Recovered_Amt_Ex_VAT__c field of the parent Payment__c to update when the child Payment__c status is updated to 'Cleared'. I have built the below code but it is updating the parent Payment__c regardless of the status.

NOTE: there will be nested child Payment__c scenarios.

//Rolls up the related payment total amounts to parent payment record // 
trigger RecoveredAmountOnPayment 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,Recovered_Amt_Ex_VAT__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.Recovered_Amt_Ex_VAT__c = val;
    }
    update lstPay;
}

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

I think I might have resolved it with the below. Would be great if someone could just let me know if I need to be aware of any potential issues with this. Sorry I'm a newby with code so I'm learning as I go

//Rolls up the related payment total amounts to parent payment record // 
trigger RecoveredAmountOnPayment 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 && p.Payment_Status__c =='Cleared'){
                    setID.add(p.Replaced_Payment__c);     
                }
            } 
            setID.add(trigger.oldmap.get(p.id).Replaced_Payment__c);
         }
    }
    if(setid.size() > 0){
        lstPay = [Select id,Recovered_Amt_Ex_VAT__c ,(Select id,Payment_Amount__c from Replaced__r WHERE Payment_Status__c = 'Cleared') 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.Recovered_Amt_Ex_VAT__c = val;
    }
    update lstPay;
}