You need to sign in to do that
Don't have an account?
thekid12345
Trouble with getting all rows queried for tasks for batch class
The goal is to gather all completed tasks who record type is Implementation Worklog. For example, say I have 3 tasks that are of the type 'Troubleshooting' and each task has a number of hours associated with each task. (i.e. Task 1 6hrs troubleshooting, Task 2 5hrs troubleshooting, Task 3 2hrs torubleshooting.) . I want to sum up the tasks so 14 hours and put the sum into a field on the parent record, which I believe I am doing in the code below. The issue is that not all appropriate parent records are being aggregated correctly. When I check Apex Jobs, it says only 9 jobs have ran when executing anonymous.
global class ProServAutomationBatch implements Database.Batchable<SObject>, Schedulable, Database.Stateful{ Id TaskLogRecordTypeId = Schema.SObjectType.Task.getRecordTypeInfosByName().get('Implementation Worklog').getRecordTypeId(); String comp = 'Completed'; private static Map<Id, Decimal> proServ_HoursSpent = new Map<Id, Decimal>(); private static Map<Id, Decimal> onBoarding_HoursSpent = new Map<Id, Decimal>(); private static Map<Id, Decimal> troubleShooting_HoursSpent = new Map<Id, Decimal>(); private static Map<Id, Decimal> internal_HoursSpent = new Map<Id, Decimal>(); Decimal proServ_currentHoursSpent; Decimal onBoarding_currentHoursSpent; Decimal troubleShooting_currentHoursSpent; Decimal internal_currentHoursSpent; Decimal proServ_hoursPerProject = 0; Decimal totalProServ = 0; //Decimal proServ_HoursSpent; //Decimal onBoarding_HoursSpent; //Decimal troubleShooting_HoursSpent; //Decimal internal_HoursSpent; //Set<Id> impProjectIds = new Set<Id>(); global Database.QueryLocator start(Database.BatchableContext BC){ //Queries for all Task whose status is completed and where the Task record type is Implementation Worklog //return Database.getQueryLocator([SELECT ProServ_Hours_Spent__c, WhatId, Implementation_Type__c FROM Task WHERE Status = :comp AND WhatId IN :implementationProjectToTask.keySet() AND RecordTypeId = :TaskLogRecordTypeId]); return Database.getQueryLocator([SELECT ProServ_Hours_Spent__c, WhatId, Implementation_Type__c FROM Task WHERE Status = :comp AND RecordTypeId = :TaskLogRecordTypeId ORDER BY WhatId]); } // Execute Logic global void execute(Database.BatchableContext BC, List<Task> workLogTasks){ Set<Id> impProjectIds = new Set<Id>(); List<Implementation_Project__c> iP = [SELECT ProServ_Hours_Spent__c,Implementation_Hours_Spent__c, Troubleshooting_Hours__c,Internal_Project_Hours_Spent__c FROM Implementation_Project__c WHERE id in :impProjectIds]; for(Task t : workLogTasks){ Decimal hoursTotal = proServ_HoursSpent.get(t.WhatId) == null ? 0 : proServ_HoursSpent.get(t.WhatId); Decimal hoursTotal2 = troubleShooting_HoursSpent.get(t.WhatId) == null ? 0 : troubleShooting_HoursSpent.get(t.WhatId); Decimal hoursTotal3 = onBoarding_HoursSpent.get(t.WhatId) == null ? 0 : onBoarding_HoursSpent.get(t.WhatId); Decimal hoursTotal4 = internal_HoursSpent.get(t.WhatId) == null ? 0 : internal_HoursSpent.get(t.WhatId); if(t.Implementation_Type__c == 'ProServ') { proServ_currentHoursSpent = t.ProServ_Hours_Spent__c; //impProjectIds.add(t.WhatId); if (proServ_HoursSpent != null){ proServ_HoursSpent.put(t.WhatId, hoursTotal + proServ_currenthoursSpent); System.debug('ProServ:' + proServ_HoursSpent); } impProjectIds.add(t.WhatId); } //System.debug('ProServ:' + proServ_currentHoursSpent); if(t.Implementation_Type__c == 'Troubleshooting') { troubleShooting_currentHoursSpent = t.ProServ_Hours_Spent__c; //impProjectIds.add(t.WhatId); if (troubleShooting_HoursSpent != null){ troubleShooting_HoursSpent.put(t.WhatId, hoursTotal2 + troubleShooting_currentHoursSpent); } impProjectIds.add(t.WhatId); } System.debug('TroubleShooting:' + troubleShooting_HoursSpent); if(t.Implementation_Type__c == 'Onboarding') { onBoarding_currentHoursSpent = t.ProServ_Hours_Spent__c; //impProjectIds.add(t.WhatId); if (onBoarding_HoursSpent != null){ onBoarding_HoursSpent.put(t.WhatId, hoursTotal3 + onBoarding_currentHoursSpent); } impProjectIds.add(t.WhatId); } System.debug('Onboarding:' + onBoarding_HoursSpent); if(t.Implementation_Type__c == 'Internal') { internal_currentHoursSpent = t.ProServ_Hours_Spent__c; //impProjectIds.add(t.WhatId); if (internal_HoursSpent != null){ internal_HoursSpent.put(t.WhatId, hoursTotal4 + internal_currentHoursSpent); } impProjectIds.add(t.WhatId); } System.debug('Internal:' + internal_HoursSpent); } for(Implementation_Project__c impProject : iP){ impProject.ProServ_Hours_Spent__c = NULL; impProject.Implementation_Hours_Spent__c = NULL; impProject.Troubleshooting_Hours__c = NULL; impProject.Internal_Project_Hours_Spent__c = NULL; impProject.ProServ_Hours_Spent__c = proServ_HoursSpent.get(impProject.Id); impProject.Implementation_Hours_Spent__c = onBoarding_HoursSpent.get(impProject.Id); impProject.Troubleshooting_Hours__c = troubleShooting_HoursSpent.get(impProject.Id); impProject.Internal_Project_Hours_Spent__c = internal_HoursSpent.get(impProject.Id); //System.debug('Internal' + internal_currentHoursSpent); System.debug('ProServ' + proServ_currentHoursSpent); //System.debug('Onboarding' + onBoarding_currentHoursSpent); //System.debug('Troubleshooting' + troubleShooting_currentHoursSpent); } Database.update(iP, false); } global void execute(SchedulableContext sc){ } global void finish(Database.BatchableContext BC){ // Logic to be Executed at finish } }