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
sfdc@isha.ax1814sfdc@isha.ax1814 

Rollupsummary Sum Agreate Triger error

REQUIREMENT: The developer has been mandated to use Apex to aggregate the hours on the projects whenever a timesheet entry is created or updated. 1. Create the custom objects in the org with these custom fields Object Project Timesheet Fields Total Billable Hours (Number (3,2)) Project (LOOKUP to Project) Total Non-Billable Hours (Number (3,2)) Hours (Number (3,2)) Billable (Boolean) 2. Create a trigger (“AFTER INSERT, AFTER UPDATE”) on TIMESHEET a. Update the parent project’s Total Billable Hours with the SUM of all related Timesheets hours where “billable=true” b. Update the parent project’s Total Non-Billable Hours with the SUM of all related Timesheets hours where “billable=false



trigger RollUpFromChildToParent on Timesheet__c (after insert, after update) {
     
    Set<Id> prjtIds = new Set<Id>();
    Map<Id,Project__c> projectMapToUpdate = new Map<Id,Project__c>();  
 
    if(Trigger.isInsert || Trigger.isUpdate){
        for(Timesheet__c th : Trigger.new){
            if(th.Project__c != null){
                prjtIds.add(th.Project__c); 
            }
        }
    }
     
     
    List<Project__c> PrjtList = new List<Project__c>();
    List<AggregateResult> AggregateResultList= [select project__c,Sum(Hours__c)tbh from Timesheet__c where project__c in:prjtIds group by Billable__c]; 
    
     if(AggregateResultList != null && AggregateResultList.size() > 0){ 
        for(AggregateResult aggr:AggregateResultList){   
        
         if (String.valueOf(aggr.get('Billable__c'))=='True')
            {
         project__c pr1=new project__c();
        pr1.Id = (Id)aggr.get('Project__c');
        pr1.Total_Billable_Hours__c = (Decimal)aggr.get('tbh');
        projectMapToUpdate.put(pr1.Id, pr1);
        }
        else{
         project__c pr1=new project__c();
        pr1.Total_Non_Billable_Hours__c = (Decimal)aggr.get('tbh');
        projectMapToUpdate.put(pr1.Id, pr1);
        }
    // PrjtList.add(pr1);
     }
    try{
        update projectMapToUpdate.values();
    }catch(DmlException de){
        System.debug(de);
    }
}
}

My logic is not working. Please help me

Regards,
Isha
AnkaiahAnkaiah (Salesforce Developers) 
Hi Isha,

Try with below code.
trigger rollupts on Timesheet__c (after delete,after update,after insert,after undelete) {
    
    set<ID> PrjIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(Timesheet__c ts : trigger.new){
            PrjIds.add(ts.Project__c);
        }
    }
    if(trigger.isdelete){
        for(Timesheet__c ts : trigger.old){
            PrjIds.add(ts.Project__c);           
        }        
    }
       
    if(trigger.isupdate){
        for(Timesheet__c ts:trigger.new){
            PrjIds.add(ts.Project__c);
            if(trigger.oldmap.get(ts.id).Project__c != ts.Project__c && trigger.oldmap.get(ts.id).Project__c != null ){
                PrjIds.add(trigger.oldmap.get(ts.id).Project__c);
            }            
        }
    }    
    map<id,double> amtmap = new map<id,double>();
    for(aggregateresult ag : [select Project__c ,SUM(Hours__c) SOH,count(id) cc from Timesheet__c where Project__c in:PrjIds AND Billable__c = TRUE group by Project__c]){
        amtmap.put((ID)ag.get('Project__c'), double.valueof(ag.get('SOH')));
       // amtmap.put((ID)ag.get('Project__c'), double.valueof(ag.get('cc')));
    }
    list<Project__c> prjlist = new list<Project__c>();
   
    for(id iid : PrjIds){
        Project__c pr = new Project__c(id=iid);
        if(amtmap.containskey(iid)){
            pr.Total_Opty_Amount_C = amtmap.get(iid);
        }else{
            pr.Total_Billable_Hours__c = 0;
        } 
        prjlist.add(pr);       
    }
   
        update prjlist;
   
}

If this helps, Please mark it as best answer.

Thanks!!