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 

Validation Rule is causing problems with my Trigger

Hi guys,

I have created a validation rule to make a date field mandatory on a custom Payment__c object when certain values in a picklist are selected. When I activate the rule, it causes issues on records which have a lookup field populated. Below is my trigger. Can anyone see where I am going wrong?

//Rolls up the related payment total amounts to parent payment record // 
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;
}
PRAKASH JADA 13PRAKASH JADA 13
In your trigger, you are doing an update. If the date value is blank the validation will throw an error. so before you do update make sure that the date field is not empty.