Search Community
- Force.com Discussion Boards
- :
- Salesforce User Discussions
- :
- Best Practices Discussion
- :
- Re: No expected result
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
No expected result
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
03-22-2010 04:29 PM
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 =
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 =
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;
}
}
Re: No expected result
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
03-22-2010 05:02 PM
Re: No expected result
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
03-22-2010 05:19 PM

