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
GhanesanGhanesan 

test coverage 38%

Requirement: 
SOQL:  
1.account rating = prospect   
2.account didn't have any opportunities last 2 years
3.account didn't have any tasks last 2 years    
Action: 
get this related account details and contact details   
1.update account rating as dormant 

Batch Class:

global class opptask implements Database.Batchable <sobject>{ public List <Account> acclist = new List <Account>();
public List <id> taskList = new List<id>();
global Database.QueryLocator start(Database.BatchableContext bc) { string query = 'SELECT accountid FROM Task WHERE What.Type = Account AND CreatedDate = LAST_N_MONTHS:48';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Task> scope)
{
for(Task tsk : scope)
{
taskList.add(tsk.whatId);
}
acclist = [SELECT Id, Name FROM account WHERE Rating = 'Prospect' AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:48) AND Id NOT IN :taskList];
for (Account a : acclist)
{
a.Rating = 'dormant';
}
Update acclist;
}
global void finish(Database.BatchableContext bc) { }
}

Test Class:

@isTest
Private Class opptasktest {

   Static testMethod void testBatchExecuteMethod()
   {
     Account a = new Account();
     a.Name = 'John Smith';
     a.Rating = 'Prospect';

     insert a;

     Test.startTest();

     opptask batchTest = new opptask ();
     Id jobid = Database.executeBatch(batchTest,5);

     Test.stopTest();

     Account acc = [Select Rating from Account where id=: a.Id];

     System.assertEquals('Dormant', acc.Rating);
    }
}
 
Best Answer chosen by Ghanesan
AnkaiahAnkaiah (Salesforce Developers) 
Hi Ganesan,

you need to modify your query in the batch apex.
 
global class opptask implements Database.Batchable <sobject>{ 
    
public List <Account> acclist = new List <Account>();
public List <id> taskList = new List<id>();
global Database.QueryLocator start(Database.BatchableContext bc) { 
    string query = 'SELECT accountid FROM Task WHERE What.Type = \'Account\' AND CreatedDate = LAST_N_MONTHS:48';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Task> scope)
{
for(Task tsk : scope)
{
taskList.add(tsk.whatId);
}
acclist = [SELECT Id, Name FROM account WHERE Rating = 'Prospect' AND Id NOT IN (SELECT accountid FROM Opportunity WHERE CreatedDate = LAST_N_MONTHS:48) AND Id NOT IN :taskList];
for (Account a : acclist)
{
a.Rating = 'dormant';
}
Update acclist;
}
global void finish(Database.BatchableContext bc) { }
}

and then try with below test class.
@isTest
public class OppTaskTest {
    @isTest
    public static void unit_test(){
       //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.Rating='prospect';
        insert acc;
        
       Account acc1 =new Account();
        acc1.Name = 'test';
        acc1.Rating='prospect';
        insert acc1;
   
        
        //insert All mandatory fields
        task t = new task();
        t.subject = 'test acc task';
        t.whatid = acc.Id;
        insert t;
        Test.setCreatedDate(t.Id, DateTime.now().addMonths(-48));
         
       
        
        Test.startTest();
      
        opptask uar  = new opptask();
        Database.executeBatch(uar);

        Test.stopTest();
       

 }
}

If this helps, Please mark it as best answer.

Thanks!!