You need to sign in to do that
Don't have an account?
sfdc@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
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
Try with below code.
If this helps, Please mark it as best answer.
Thanks!!