You need to sign in to do that
Don't have an account?

Bulkification of the trigger
trigger Update_Actualtime_Projecttask on Timesheet__c(after insert, after update)
{
for(Timesheet__c t:trigger.new)
{
Project_Task__c p =[select id,Project__c , Actual_Time__c from Project_Task__c where id=:t.Project_Task__c and project__c=:t.project__c];
Timesheet__c[] t1=[select id,Effort_in_hours__c from Timesheet__c where project__c =:p.project__c and Project_Task__c =:p.id];
Decimal id2num =0.0;
p.Actual_Time__c=0;
for(Timesheet__c t2:t1)
{
id2num =t2.Effort_in_hours__c + id2num;
p.Actual_Time__c = id2num;
update p;
}
}
}
bulkification of the trigger
Just a tip, you should always avoid putting Select and DML statements inside For loop so that you don't run into the query/DML gov limit.
To be clear, you are trying to compute the total time from timesheet and update the respective project with the number. You could rewrite the code as
trigger Update_Actualtime_Projecttask on Timesheet__c(after insert, after update)
{
List <Id> pTaskId = new List <Id>();
for(Timesheet__c t:trigger.new)
{
if(t.Project_Task__c !=null)
pTaskId.add(t.Project_Task__c );
}
Project_Task__c p =[select id,Project__c , Actual_Time__c from Project_Task__c where id In :pTaskId];
Timesheet__c[] t1=[select id,Effort_in_hours__c from Timesheet__c where Project_Task__c in :pTaskId];
for(Project_Task__c pt:p)
{
Decimal id2num =0.0;
p.Actual_Time__c=0;
for(Timesheet__c ts:t1){
if(pt.Id == ts.Project_Task__c && pt.project__c == ts.project__c){
id2num =t2.Effort_in_hours__c + id2num;
p.Actual_Time__c = id2num;
}
}
}
update p;
}
All Answers
Just a tip, you should always avoid putting Select and DML statements inside For loop so that you don't run into the query/DML gov limit.
To be clear, you are trying to compute the total time from timesheet and update the respective project with the number. You could rewrite the code as
trigger Update_Actualtime_Projecttask on Timesheet__c(after insert, after update)
{
List <Id> pTaskId = new List <Id>();
for(Timesheet__c t:trigger.new)
{
if(t.Project_Task__c !=null)
pTaskId.add(t.Project_Task__c );
}
Project_Task__c p =[select id,Project__c , Actual_Time__c from Project_Task__c where id In :pTaskId];
Timesheet__c[] t1=[select id,Effort_in_hours__c from Timesheet__c where Project_Task__c in :pTaskId];
for(Project_Task__c pt:p)
{
Decimal id2num =0.0;
p.Actual_Time__c=0;
for(Timesheet__c ts:t1){
if(pt.Id == ts.Project_Task__c && pt.project__c == ts.project__c){
id2num =t2.Effort_in_hours__c + id2num;
p.Actual_Time__c = id2num;
}
}
}
update p;
}
Hi,
If you want to make your trigger bulkify then you need to make use of collection classes like List and Map in your trigger .Also try to bring your SOQL outside for loop.
Note: There is a Governor Limit to fire 100 SOQL queries.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Ok..working on it.