I have a problem with the below code which in turn should run in the form of a Before Insert Trigger on my object Payment Details. This code acrtually compiles with a Before Insert, Before Update Trigger - but the actual calculation or field update doesnot happen. Can you help ?
To give a further background:
Payment_Details
object (Child) has a lookup relationship with Payment_Plan
(Parent) object which in turn has a lookup relationship with Unit_Detail object
(Parent of Payment Plan).
Payment Details has 2 Fields a) Booking_Amount_Due__c
and b) Foundation_Amount_Due
Either of these 2 fields should get updated based on the Picklist value field in Payment Plan object Payment_Milestone__c
. Picklist value could be Booking or Foundation.
Value of the Booking_Amount_Due
or Foundation_Amount_Due
fields gets calculated
from the field Base_Apartment_Cost__c
in the object Unit Detail.
Trigger:
trigger UpdateAmtDue on Payment_Details__c (before insert, before update) {
Payment_Details__c []PD = Trigger.new;
MileAmtDUEUpdate.CurMileAmtUpdate(PD);
}
Code as below
Public class MileAmtDUEUpdate
{
public static void CurMileAmtUpdate (Payment_Details__c [] PD ) {
List<Id> PPIds = new List<Id>();
For (Payment_Details__c l :PD)
{
PPIds.add (l.Id);
}
List<Payment_Details__c> PayDet =
[Select ID, Payment_Plan__c ,Booking_Amount_Due__c , Foundation_Amount_Due__c ,
Payment_Plan__r.Payment_Milestone__c ,
Payment_Plan__r.Unit_Name__r.Base_Apartment_Cost__c from Payment_Details__c
where id = : PPIds ];
List<Payment_Details__c> NewPayDet = New List<Payment_Details__c> () ;
For (Payment_Details__c PDT : PayDet)
{ If (PDT.Payment_Plan__r.Payment_Milestone__c == 'Booking' &&
PDT.Booking_Amount_Due__c == Null)
{ PDT.Booking_Amount_Due__c =
PDT.Payment_Plan__r.Unit_Name__r.Base_Apartment_Cost__c * 0.10 ;
NewPayDet.add(PDT) ; }
else if (PDT.Payment_Plan__r.Payment_Milestone__c == 'Foundation' &&
PDT.Foundation_Amount_Due__c == Null)
{ PDT.Foundation_Amount_Due__c =
0.10 * PDT.Payment_Plan__r.Unit_Name__r.Base_Apartment_Cost__c ;
NewPayDet.add(PDT); }
}
Insert NewPayDet ;
}
}