function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to use "task.What.type" and advice on bulk updates

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).
trigger TaskTriggerAfter on Task (after insert, after update, after delete) {
 for (Task task : {
  String acctID=task.accountid;
  String caseID='';

  Task[] task2 = [select Id from task where ( 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).

Message Edited by jeremy_w on 03-29-2008 07:01 PM
No one replied....but I've worked it out now.

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.

trigger TaskTriggerAfter on Task (after insert, after update,after undelete) {
 // store all taskIds sent in
 Set<Id> tasksInSet = new Set<Id> {};
 for (Task t : {
 // filter the tasks sent in to only get ones that are related to cases
 Set<Id> casesSet = new Set<Id> {};
 for (Task t2 : [select WhatId,Id from task where (Id in :tasksInSet) AND (What.Type = 'Case')] ) {
 // get all tasks for all cases and add up the times and store against a case array
 Map<Id,Decimal> casesTimeMap = new Map<Id,Decimal> {};
 Decimal tmpTime;
 for (Task t3 : [select WhatId,Total_Time_Spent_hours__c from task where (WhatId in :casesSet)] ) {
  // if not set in array yet, create.
  if (!casesTimeMap.containsKey(t3.WhatId)) {
  tmpTime = casesTimeMap.get(t3.WhatId);
  tmpTime = tmpTime + t3.Total_Time_Spent_hours__c;
 // get all cases as objects
 Case[] updCases = [select Id,TotalTimeSpent__c from case where Id in :casesSet];
 // loop through and store updated values
 for ( Case cas : updCases ) {
  cas.TotalTimeSpent__c = casesTimeMap.get(cas.Id);

 update updCases;


Sunil Shah 12Sunil Shah 12
Hi Jeremy, I was wondering how to access what type in a SQL which i got to know from your reply. Thank you :)