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
SFHelpMePleaseSFHelpMePlease 

unable to get test enough test coverage

Could you help me with my test class, I can only get 26% coverage?
I tried googling and different test but cannot get past 26% 


global class dtCheck implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC) {
        //some records in ActivityHistory are not in Task table due to archive
        //lets capture all of the account ids
        Set<Id> ids = new Set<Id>();
        List<Account> acctList = new List<Account>();
        for(Account a : [Select Id,(Select Id, subject, whoid, whatid, accountid, createddate from ActivityHistories) FROM Account ])
        {
            if(!a.ActivityHistories.isEmpty()){
               ids.add(((Id)a.get('Id')));
               System.debug('ActivityHistory id ' + (Id)a.get('Id'));
            }
        }

        //could be dup accountIds between ActivityHistory and Task, select the distinct Ids
        Set<Id> distinctIds = new Set<Id>();
        List<AggregateResult> acctDistinctList= [SELECT Id From Account where Id in :ids group by Id];
        for (AggregateResult ar2 : acctDistinctList) {
          distinctIds.add(((Id)ar2.get('Id')));
          System.debug('Account to update ' + (Id)ar2.get('Id'));
        }

        String query = 'Select Id, Name, task_Over_90_Days__c  FROM Account  where Id in :distinctIds ';

        return Database.getQueryLocator(query);
    }


    global void execute(Database.BatchableContext BC, List<Account> scope) {
        
        // process each batch of records
        System.debug('execute batch: ' + scope.size());

        for(Account acc : scope)
        {
            integer noOfDays = 0;
            integer over90Days = 0;
         
            
            List<Task> taskList = [SELECT  Id, Subject, CreatedDate, AccountId, TaskSubtype FROM Task where AccountId = :acc.Id];
            for(Task t : taskList)
            {
                noOfDays = Date.today().daysBetween(t.CreatedDate.Date())*-1;
                if (noOfDays > 90)
                    over90Days = over90Days + 1;

            }
            List<Account> acctHistories = [Select Id,(Select Id, subject, whoid, whatid, accountid, createddate from ActivityHistories ) FROM Account where id = :acc.Id];
            for (Account a: acctHistories) {
                for (ActivityHistory ah : a.getSObjects('ActivityHistories')) 
                {
                    noOfDays = Date.today().daysBetween(ah.CreatedDate.Date())*-1;
                    if (noOfDays > 90)
                        over90Days = over90Days + 1;
                }
            }
            acc.Over_90_Days__c  = over90Days  ;
            update acc;
        }

         
    }   
      
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
  }
}






@isTest
public class dtCheckTest 
{
    static testMethod void testMethod1() 
    {
        List<Account> lstAcct = new List<Account>();
        List<Task> lstTask = new List<Task>();
        for(Integer i=0 ;i <10;i++)
        {
            Account acc = new Account();
            acc.Name ='Test Name'+i;
            lstAcct.add(acc);
        }
        insert lstAcct ;

        List<Account> acct = [Select Id, Name from Account Limit 1] ;
        for (Account a : acct)
        {
            Task tsk = new Task();
            tsk.Subject = 'Test ';
            tsk.WhatId = a.Id;
            lstTask.add(tsk);
        }
        insert lstTask ;

        

        Test.startTest();
        dtCheck obj = new dtCheck();
        DataBase.executeBatch(obj,10); 
        Test.stopTest();
    }
}
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I'd like to help you make the code do what I think you intend it to do.  I don't think it works they way you want.  In the start method, the first section of code creates a set called 'ids'.  This will have the Ids of any Accounts that have any sort of ActivityHistory record (new, old, Task, Event, etc).  So your execute method won't see any Accounts that have Tasks but don't have any ActivityHistories.  The second section of code creates a set called 'distinctIds'.  I've read that code several times and I can gaurantee you that 'distinctIds' will be exactly the same as 'ids' -- so that section of code can be deleted.  Because you're doing full-table queries in the start method, the code won't work in an org with more than 10,000 Account records.  In the execute method, the code is not bulkified, with SOQL queries and DML inside the for loop.  This will hit governor limits.  I notice your test uses a batch size of 10 to avoid this.  I believe the code below meets your requirements.  It is much simpler and should be very easy to cover 100%.  I hope this helps.

<pre>
global class dtCheck implements Database.Batchable<sObject>
{
    global Database.QueryLocator start( Database.BatchableContext bc )
    {
        return Database.getQueryLocator( 'SELECT Id FROM Account' );
    }

    global void execute( Database.BatchableContext bc, List<Account> scope )
    {
        List<Account> accounts = List<Account>();
        for ( Account account :
            [   SELECT  Id, Over_90_Days__c,
                    (   SELECT  Id
                        FROM    Tasks
                        WHERE   CreatedDate < LAST_90_DAYS
                    ),
                    (   SELECT  Id
                        FROM    ActivityHistories
                        WHERE   (   IsTask = true
                                AND CreatedDate < LAST_90_DAYS
                                )
                    )
                FROM    Account
                WHERE   Id IN :scope
            ]
            )
        {
            account.Over_90_Days__c =
            (   account.Tasks.size()
            +   account.ActivityHistories.size()
            );
            accounts.add( account );
        }
        update accounts;
    }

    global void finish(Database.BatchableContext BC)
    {
        //  nothing to do here...
    }
}
</pre>
SFHelpMePleaseSFHelpMePlease
Thank you very much for reviewing! I shortened the code I had before sending it. I need to grab the createdDate and decide if it is 30days, 60days or over 90days Is there a way to grab it then create some if statements to decide what bucket it falls in?