You need to sign in to do that
Don't have an account?
jeremy_w
Message Edited by jeremy_w on 03-29-2008 07:01 PM
How to use "task.What.type" and advice on bulk updates
Hi
I've created this trigger to update the total time for a case based on the times in all the tasks (custom fields).
But I couldn't work out how to use "What.type" to only run the code for tasks associated to cases (I've got a workaround in there as you can see).
Code:
trigger TaskTriggerAfter on Task (after insert, after update, after delete) { for (Task task : Trigger.new) { String acctID=task.accountid; String caseID=''; Task[] task2 = [select Id from task where (Id=:task.id) AND (What.Type = 'Case')]; if (task2.size()>0) { caseID = task.WhatId; Task[] tasks = [select Total_Time_Spent_hours__c from task where WhatId=:caseID]; Decimal TotalTimeSpent = 0; for (Task t : tasks) { TotalTimeSpent = TotalTimeSpent + t.Total_Time_Spent_hours__c; } Case cas = [select Id,TotalTimeSpent__c from Case WHERE Id=:caseID]; cas.TotalTimeSpent__c = TotalTimeSpent; update cas; } } }
I thought:
if (task.What.type == 'Case') {
would work (replacing the task2 stuff) but it didn't.
So I tried to find out if I got a result from the query but couldn't unless I did it using size (even though only 1 result max will be returned).
Maybe something based on the caseID would be possible: if (caseID.length() )? (I'm not sure about the string functions).
If anyone has advice on more efficient ways to code this then please do let me know. Also any links to descriptions of string functions in Apex as I can't find any (such as length).
And finally, I've read that you should code triggers to allow for bulk updates, but when will/how can bulk updates occur? (I tried to do a replacement of a picklist value but that only seems to replace the value and the last modified date - it doesn't run the triggers).
Thanks.
Message Edited by jeremy_w on 03-29-2008 07:01 PM
Please see below if anyone's interested - I've changed it so it works for bulk updates and think it's fine now. This also solved the problem of the what.type in the if statement.
It's quite likely this can be simplified or improved; it's my first attempt at using Sets and Maps.