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
bohemianguy100bohemianguy100 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Rahul S.ax961Rahul S.ax961

Hi bohemianguy100,

 

I went through your code and made some optimization,

 

 

private static void doHoursCompletedHoursRemaining(List<Task> inNew)
{
    List<Project_Management_Task_List__c> PMTLsToUpdate = new List<Project_Management_Task_List__c>();
    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);
            }
        }
    }
    if (pmtlIds.size() > 0)
    {
  
     for (Project_Management_Task_List__c pmtl : [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])
        {
            Decimal Remaining = 0;
            Decimal Completed = 0;
            if (pmtl.Tasks.size() > 0)
            {
                for (Task t : pmtl.Tasks)
                {
                    if(t.Hours_Remaining__c != '')
                        Remaining += t.Hours_Remaining__c;
                    if(t.Hours_Completed__c != '')
                        Completed += t.Hours_Completed__c;
                }
            }
            pmtl.Hours_Remaining__c = Remaining;
            pmtl.Actual_Hours_Completed__c = Completed;
            PMTLsToUpdate.add(pmtl);
        }
        if(PMTLsToUpdate.size() > 0)
            update PMTLsToUpdate;
    }
}

 

 

Hope it helps. let me know if you face any issues. :)

All Answers

Rahul S.ax961Rahul S.ax961

Hi bohemianguy100,

 

I went through your code and made some optimization,

 

 

private static void doHoursCompletedHoursRemaining(List<Task> inNew)
{
    List<Project_Management_Task_List__c> PMTLsToUpdate = new List<Project_Management_Task_List__c>();
    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);
            }
        }
    }
    if (pmtlIds.size() > 0)
    {
  
     for (Project_Management_Task_List__c pmtl : [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])
        {
            Decimal Remaining = 0;
            Decimal Completed = 0;
            if (pmtl.Tasks.size() > 0)
            {
                for (Task t : pmtl.Tasks)
                {
                    if(t.Hours_Remaining__c != '')
                        Remaining += t.Hours_Remaining__c;
                    if(t.Hours_Completed__c != '')
                        Completed += t.Hours_Completed__c;
                }
            }
            pmtl.Hours_Remaining__c = Remaining;
            pmtl.Actual_Hours_Completed__c = Completed;
            PMTLsToUpdate.add(pmtl);
        }
        if(PMTLsToUpdate.size() > 0)
            update PMTLsToUpdate;
    }
}

 

 

Hope it helps. let me know if you face any issues. :)

This was selected as the best answer
bohemianguy100bohemianguy100

Thank you Rahul...worked perfectly! I appreciate your help!