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
System Administrator 393System Administrator 393 

Trigger for code not updating the value of the field

I have customized objects Bills andContract. A contract has many bills. Here is the code to calculate revenue from bills based on their transaction dates and update a field on the contract. 

Here is the class:

public class UpdateRunRateonloanaccount{

public static decimal getbills(loan__Loan_Account__c a){

decimal d = 0.00;
decimal d1 = 0.00;
decimal d2 = 0.00;
decimal d3 = 0.00;
decimal d4 = 0.00;
decimal d5 = 0.00;

List<loan__Loan_account_Due_Details__c> bill1 = [SELECT Id, Account_Name__c, CurrencyIsoCode, loan__Loan_Account__c, loan__Transaction_Date__c,Revenue_Recurring_for_Period__c,Metrics_Good__c, Participation_Revenue_for_Period__c FROM loan__Loan_account_Due_Details__c WHERE loan__Loan_Account__c =: a.Id];

for(loan__Loan_account_Due_Details__c b : bill1){

if(b.loan__Transaction_Date__c > (Date.today()-30)){
 d = b.Participation_Revenue_for_Period__c;
}

if((b.loan__Transaction_Date__c < Date.today()-30) && (b.loan__Transaction_Date__c > Date.today()-60)){

d1 = b.Participation_Revenue_for_Period__c;
}

if((b.loan__Transaction_Date__c < Date.today()-60) && (b.loan__Transaction_Date__c > Date.today()-90)){

d2 = b.Participation_Revenue_for_Period__c;
}

if((b.loan__Transaction_Date__c < Date.today()-90) && (b.loan__Transaction_Date__c > Date.today()-120)){

d3 = b.Participation_Revenue_for_Period__c;
}

if((b.loan__Transaction_Date__c < Date.today()-120) && (b.loan__Transaction_Date__c > Date.today()-150)){

d4 = b.Participation_Revenue_for_Period__c;

}



if((b.loan__Transaction_Date__c < Date.today()-150) && (b.loan__Transaction_Date__c > Date.today()-180)){

d5 = b.Participation_Revenue_for_Period__c;

}
}

if(d == 0.00 && d1 > 0 && d2 > 0 && d3 > 0){
a.Sum_of_last_3_months_recurring_revenues__c = ((d1 + d2 + d3)*4);
}
else if (d > 0.00 && d1 > 0 && d2 > 0){
a.Sum_of_last_3_months_recurring_revenues__c = ((d + d1 + d2)*4);


else if (d == 0.00 && d1 == 0.00 && d2 > 0.00 && d3 > 0.00 && d4 > 0.00){
a.Sum_of_last_3_months_recurring_revenues__c = ((d2 + d3 + d4)*4);
}

else if (d == 0.00 && d1 == 0.00 && d2 == 0.00 && d3 > 0.00 && d4 > 0.00 && d5 > 0.00){
a.Sum_of_last_3_months_recurring_revenues__c = ((d3 + d4 + d5)*4);
}
return a.Sum_of_last_3_months_recurring_revenues__c;
}
}

Here is the trigger:

trigger UpdateSum on loan__Loan_Account__c (before insert,before update) {
    
        for (loan__Loan_Account__c c : Trigger.new) {
        
       if(c.loan__Loan_Amount__c != null){
    
        
        UpdateRunRateonloanaccount.getbills(c);
        
        }
      
  }
}

The field on the CLContract does not update wih the required value, I believe that there is something wrong with the trigger because the field doesnot update.