You need to sign in to do that
Don't have an account?
problem running code in bulk mode
I have a trigger that fires on Task after insert, update and delete. The trigger calls a static method that passes in the trigger.new collection.
The trigger code runs fine if I'm editing just one Task record at a time, but if I use dataloader to update in bulk, then it doesn't process correctly.
Here is my method:
private static void doHoursCompletedHoursRemaining(List<Task> inNew) { Set<Id> pmtlIds = new Set<Id>(); for (Task t : inNew) { String stringWhatId = '' + t.WhatId; if (t.WhatId != null) { if (stringWhatId.substring(0,3) == 'a0f') { pmtlIds.add(t.WhatId); } } } //Gather all Data List<Project_Management_Task_List__c> lstPMTLs = new List<Project_Management_Task_List__c>([Select Id, Hours_Remaining__c, Actual_Hours_Completed__c, (Select Id, Hours_Completed__c, Hours_Remaining__c From Tasks) From Project_Management_Task_List__c Where id in :pmtlIds]); if (lstPMTLs.size() > 0) { Map<Id, Task> mapTask = new Map<Id, Task>([Select WhatId, Hours_Remaining__c, Hours_Completed__c From Task Where WhatId in :lstPMTLs]); List<Project_Management_Task_List__c> PMTLsToUpdate = new List<Project_Management_Task_List__c>(); for (Project_Management_Task_List__c pmtl : lstPMTLs) { Decimal Remaining = 0; Decimal Completed = 0; if (pmtl.Tasks.size() > 0) { for (Task t : mapTask.values()) { for (Integer i = 0; i < lstPMTLs.size(); i++) { Remaining += mapTask.get(t.Id).Hours_Remaining__c; Completed += mapTask.get(t.Id).Hours_Completed__c; } } } pmtl.Hours_Remaining__c = Remaining; pmtl.Actual_Hours_Completed__c = Completed; PMTLsToUpdate.add(pmtl); } update PMTLsToUpdate; } }
All the code is doing is totaling the Hours Completed and Hours Remaining of all Tasks related to a specific Project Management Task List record.
If I run in bulk, it totals all the tasks records and doesn't differentiate between different Project Management Task List records. I was trying to use the WhatId as the key in the map and then use the containsKey method, but that didn't seem to work?
Thanks for any help.
Hi bohemianguy100,
I went through your code and made some optimization,
Hope it helps. let me know if you face any issues. :)
All Answers
Hi bohemianguy100,
I went through your code and made some optimization,
Hope it helps. let me know if you face any issues. :)
Thank you Rahul...worked perfectly! I appreciate your help!