Reply
Regular Contributor
Ckevin1984
Posts: 19

No expected result

I have two objects. The first one is Timesheet, it contains the Hours__c field. The other object is Project, it will have a rollup all the Hours__c in the Timesheet object in its Total_Hours__c field. And Project is the prarent object of Timesheet. I cannot get anything in my Total_Hours_c field. Here is my entire code. Thanks for any help.

trigger TimeSheetTrigger on Timesheet__c (after delete, after insert,after update)
{
double sumTotalHours = 0.0;
SFDC_Projects__c [] sheetsToUpdate = new SFDC_Projects__c[]{};

//***********************************************
//Code for updating existing records and new records
//***********************************************

if(Trigger.isInsert)
{
Timesheet__c [] teNew = trigger.new;

for(Timesheet__c te : teNew)
{
for (SFDC_Projects__c timesheet : [select Id,Name, Total_Hours__c from SFDC_Projects__c where Id = :te.Timesheet__c])
{

//Sum all the timesheet entries
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Timesheet__c = :timesheet.id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Total_Hours__c = sumTotalHours;

//add timesheet to list to be updated outside of the loop
sheetsToUpdate.add(timesheet);
}
}

//commit the changes to Salesforce
update sheetsToUpdate;
}

//***********************************************
//Code for updating when a record is updated
//***********************************************

else if(Trigger.isUpdate)
{
//sum total both old and new
Timesheet__c [] oldTime = Trigger.old;
Timesheet__c [] newTime = Trigger.new;
Double newSum = 0.0;
Double oldSum = 0.0;

for(Timesheet__c newTe: newTime)
{
for(Timesheet__c oldTe : oldTime)
{

SFDC_Projects__c oldTimesheet = [Select Id, Name, Total_Hours__c from SFDC_Projects__c where Id = :smileysurprised:ldTe.Timesheet__c];
SFDC_Projects__c newTimesheet = [Select Id, Name, Total_Hours__c from SFDC_Projects__c where Id = :newTe.Timesheet__c];

Timesheet__c [] newSumHours = [Select Id, Name, Hours__c  from Timesheet__c where Timesheet__c = :newTimesheet.Id];
Timesheet__c [] oldSumHours = [Select Id,Name, Hours__c from Timesheet__c where Timesheet__c = :smileysurprised:ldTimesheet.Id];

//sum premiums from child objects
for(Timesheet__c oldSumHour : oldSumHours)
{
oldSum += oldSumHour.Hours__c;
}

for(Timesheet__c newSumHour : newSumHours)
{
newSum += newSumHour.Hours__c;
}

newTimesheet.Total_Hours__c = newSum;
oldTimesheet.Total_Hours__c = oldSum;

sheetsToUpdate.add(newTimesheet);
sheetsToUpdate.add(oldTimesheet);
}
}

update sheetsToUpdate;
}

//***********************************************
//Code for updating when a record is deleted
//***********************************************

else if(Trigger.isDelete)
{

Timesheet__c [] teOld = trigger.old;

for(Timesheet__c te: teOld)
{

for (SFDC_Projects__c timesheet: [select Id, Name, Total_Hours__c from SFDC_Projects__c where Id = :te.Timesheet__c])
{
for (Timesheet__c timeEntries: [select Id, Hours__c from Timesheet__c where Timesheet__c = :timesheet.id])
{
sumTotalHours += timeEntries.Hours__c;
}

timesheet.Total_Hours__c = sumTotalHours;

sheetsToUpdate.add(timesheet);
}
}

update sheetsToUpdate;
}    
}

Regular Contributor
flewells
Posts: 92

Re: No expected result

Have you tried creating a Roll-Up Summary custom field on Project that summarizes information from Timesheet?  Unless your trigger is summing hours from the related Timesheets in a really specific way (sorry, I'm not familiar with writing triggers, so I'm not able to tell for myself), this should be native functionality availble to you (because Project has a Master-Detail relationship with Timesheet).
Regular Contributor
Ckevin1984
Posts: 19

Re: No expected result

I have created the Roll-up summary field in the Project and i have made  them in the Master-Detail relationship