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
Miranda L 2Miranda L 2 

Batch Apex is not working as expected

Hello there,
I have written batch class which is not working as expected so could you please help me to findout the issue.

 
Miranda L 2Miranda L 2
Here is my Batch Apex class
 
global class BatchUpdateActivityInfoAccounts implements Database.Batchable<sObject> {
    
    global Map <id,Account> mapacc;   
    global Datetime closest_date (List <Datetime> t){
        datetime aux;
        for(Datetime tt: t) {
           if(aux == null) aux = tt;
           else if (system.now().gettime() - tt.gettime() < system.now().gettime()- aux.gettime()) aux = tt;
        }
        return aux;
    }
    
    
    global Database.QueryLocator start(Database.BatchableContext theContext) {
        
    	String s = (System.Test.isRunningTest())? 'SELECT id,True_Last_Activity__c,Next_Activity_Date__c FROM Account LIMIT 200':'SELECT id,True_Last_Activity__c,Next_Activity_Date__c FROM Account';
        return Database.getQueryLocator(s);    
    }
    
    global void execute(Database.BatchableContext theContext, List<sObject> scope) {
        mapacc = new Map <id,Account> ();
        for(Account A :(List <Account>) scope) {
            mapacc.put(A.Id,A);
        } 
        map <id,Contact> mapContacts = new Map <id,Contact> ();
        for(Contact C: [SELECT id FROM Contact WHERE Accountid in: mapacc.keySet()]) {
            mapContacts.put(C.id,C);
        }
        List <Datetime> AllactivitiesDate = new List <DateTime> ();
        //Whatid : Lead & Contact
        // WhatId: AccountId,OpportunityID,nonhuman objects such as accounts, opportunities, campaigns, cases, or custom objects. 
        List <Task> tasks = [SELECT id,Description,ActivityDate,Whatid,Status,Subject FROM Task WHERE ActivityDate != NULL AND Type != 'Record Assignment' AND (Whatid in:mapacc.keySet() OR Whoid in:mapContacts.keyset()) ORDER BY ActivityDate ASC];
        List <Task> TasksNext = new List <Task> ();
        List <Task> TasksCompleted = new List <Task> ();
        //Store in map all tasks (Whatid(acc.id),List<task>)
        for(Task T: tasks){
            //if the system date is less than Due Date or Status not completed
            if(System.now() <= T.ActivityDate || T.Status!='Completed')TasksNext.add(T);
            else TasksCompleted.add(T);
        }
       
        for(Account A : mapacc.values()) {
            boolean found = false;
            List <DateTime> dates = new List <DateTime> ();
            
            for(Task t: TasksCompleted){
                
            if(t.whatid==A.id && !t.subject.containsIgnoreCase('attempted'))
            dates.add((Datetime)t.ActivityDate);
                
            }
        A.First_Activity__C = (dates.size()>0)? Util.first_Activity(dates):NULL;
            
            dates = new List <DateTime> ();
            
            for(Task t: TasksCompleted){
                if(t.whatid==A.id)
                dates.add((Datetime)t.ActivityDate);
            }
            
            A.True_last_activity__c =(dates.size()>0)? closest_date(dates):NULL;
            dates = new List <DateTime> ();
            
            for(Task t: TasksNext) {
                if(t.whatid==A.id)
            dates.add((Datetime)t.ActivityDate);
            }
            
            A.Next_Activity_Date__c =(dates.size() > 0)? closest_date(dates):NULL;
         }	                
        Update Mapacc.values();
    }
    
    global void finish(Database.BatchableContext theContext){
    }
}

Thanks