2

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 ;  

}   
}
  • Pls provide trigger code as well – Vignex Jul 8 '15 at 10:38
  • trigger UpdateAmtDue on Payment_Details__c (before insert, before update) { Payment_Details__c []PD = Trigger.new; MileAmtDUEUpdate.CurMileAmtUpdate(PD); } – Rajiv V.Subramanian Jul 8 '15 at 10:47
1

One problem I see is in your select statement:

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 ];  

Since you want to query for all updated records you want to use the IN clause not = Also change PPIds to a set vs a list.

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 IN : PPIds ];

What you can do is a System.debug on your other fields to validate they contain the data that should match the criteria. Could it be possible that Amount defaults to 0 instead of null?

Also since it is before insert in some cases, the Id will not be available yet. Take a look at the docs here for help with trigger context considerations.

Try to instead create a map of the parent then access the map in your for loop and assign your variables.

  • New to apex - how do you do a system debug. When I see the show dependencies on MileAmtDUEUpdate class , I see that it clearly shows an Insert on Object Payment_Details and update on the fields Booking_Amount_Due and Foundation_Amount_Due. Dependies doesnot show insert on the Payment_Details object in the UpdateAmtDue class (shown in above comment) – Rajiv V.Subramanian Jul 8 '15 at 10:55
  • 1
    @RajivV.Subramanian Take a look at this, it's a nice walk through: sfdc99.com/2014/02/22/debug-your-code-with-system-debug Also just noticed you are attempting an insert on a before trigger context, you would need to instead just assign the value to the record. – Jenny B Jul 8 '15 at 11:02
  • @RajivV.Subramanian noticed another problem is that the Id will not be available on the before insert. Have you considered using the process builder for this task? – Jenny B Jul 8 '15 at 11:19
  • The id should become available from the Lookup to Payment Plan isn't it since the Lookup accesses a specific record. Do I also need to store New Payment Details records first in the set with a statement to capture the New payment details like say – Rajiv V.Subramanian Jul 8 '15 at 11:45
  • 1
    @RajivV.Subramanian yes the payment plan id is available in the payment detail record so create a set of those ids and use that to query the payment plan to create a map of the payment plan with the fields in the record you need access to. Then loop over the list of payment details you sent in and select the payment plan record from the map to obtain the values you need in your if statement and formulas. – Jenny B Jul 8 '15 at 11:55
0
Public class  NewAmtDUEUpdate 
  {
    public static void  CurMileAmtUpdate (Payment_Details__c [] PD ) {

        Set <Id> PPIds = new Set <Id>();

       For (Payment_Details__c l : PD)

      { If ( l.Payment_Plan__c != Null && l.Booking_Amount_Due__c == Null && 
            l.Foundation_Amount_Due__c == Null) 

               {l.Booking_Amount_Due__c = 0 ;
                l.Foundation_Amount_Due__c = 0 ; 
               PPIds.add (l.Id);}
                                     }

      Map <ID,Payment_Details__c> PayDetMap  

      = New Map <ID,Payment_Details__c> ([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 In : PPIds ]);


      List<Payment_Details__c> NewPayDet = New List<Payment_Details__c> () ;

      For (Payment_Details__c PDT : PD ) 

        {  Payment_Details__c P = PaydetMap.get(PDT.id) ;

         // If (P.ID != Null)
           //   { P.Booking_Amount_Due__c = 0 ;
             //  P.Foundation_Amount_Due__c = 0 ;}

          If (P.ID != Null && P.Payment_Plan__r.Payment_Milestone__c == 'Booking' &&

             P.Booking_Amount_Due__c == 0 )

     { P.Booking_Amount_Due__c = 
      P.Payment_Plan__r.Unit_Name__r.Base_Apartment_Cost__c * 0.10 ;

                  NewPayDet.add(P) ; }

     else if (P.ID != Null && P.Payment_Plan__r.Payment_Milestone__c == 'Foundation' && 
     P.Foundation_Amount_Due__c == 0 )         

   { P.Foundation_Amount_Due__c = 
   0.10 * P.Payment_Plan__r.Unit_Name__r.Base_Apartment_Cost__c ;
                  NewPayDet.add(P); }

        }
    }   
}
  • 1
    Can you update your question with new code vs posting it as an answer? I think you misunderstood my suggestion. Loop through your list and assign the Parent Payment Plan Id to a Set; create a map of Id, Payment Plan by querying for the records IN the Set above. Loop PD list again and using the payment plan id grab the record that the payment detail belongs to from the map(verify the map contain value 1st) and then add your conditions and field updates to your payment detail using values from your payment plan record you just retrieved. Payment Detail ID will not be available before insert. – Jenny B Jul 9 '15 at 2:45
  • How do you get the record id of the parent payment plan id – Rajiv V.Subramanian Jul 9 '15 at 7:57

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.