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
Chuck HarveyChuck Harvey 

Sum Custom Object and displaying the results in another table

I am trying to write a simple trigger and class to sum a column in multiple records and dispaly the results in Projects.

The trigger is designed to sum column (Total_Invoice_Amount__c(custom field)) in Time Records and display the results in column  Actual_Pre_Payment_Amount__c in Projects.

It runs but no results.

Trigger:

trigger PrePaidAmount on Milestone1_Project__c (after insert, after update) {
    List< Milestone1_Project__c > listTiv = new List< Milestone1_Project__c >();
   
     for (Milestone1_Project__c mp : Trigger.new){ 
       List<AggregateResult> dupes =[ Select Project1__c, sum(Total_Invoice_Amount__c) tiv  from  Time_Record__c
       where Project1__c = :mp.Name Group by Project1__c];
       for(AggregateResult ar : dupes){
 Integer SumAmount = (Integer) ar.get('tiv');
           if (dupes.size() > 0) { 
            Milestone1_Project__c pro = new Milestone1_Project__c ();
          // Insert new Pre-Payment into Projects record
            system.debug(mp.NAME);
            pro.Actual_Pre_Payment_Amount__c = SumAmount;
           listTiv.add(pro);}
  try{
     insert listTiv;
  }
 catch (Exception e) {
      system.debug('Exception'+e);
   }
   }
}}
SonamSonam (Salesforce Developers) 
Is your SOQL returning any records:
Select Project1__c, sum(Total_Invoice_Amount__c) tiv  from  Time_Record__c
       where Project1__c = :mp.Name Group by Project1__c]?
The reason why I asked is that the if loop seems to run only if the list has any records.