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
suji srinivasansuji srinivasan 

how to get 100% coverage for batch apex

Batch
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator start(Database.BatchableContext bc) {
      string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN (\'workInprogress,Delivered,closedwon\') AND AccountId !=Null';
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
      
        for(opportunity o :Scope){
            o.Account.Rating='client';  
          system.debug('Rating'+o.Account.Rating);
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope;}
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];  
    }   
     }

test class
@isTest
public class UpdateAccountRatingTest {
    @isTest
    public static void unit_test(){
       //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.Rating='prospect';
        insert acc;
        
        //insert All mandatory fields
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Account.Rating='prospect'; 
        opp.Accountid = acc.Id;
        opp.stageName = 'workInprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
         
       
        
        Test.startTest();
      
        UpdateAccountRating uar  = new UpdateAccountRating();
        Database.executeBatch(uar);
      String sch = '0 0 0 1 * ? *';
      string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
        Test.stopTest();
       

 }
    }

thanks in advance
Best Answer chosen by suji srinivasan
AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

your query was in correct in the bact apex that is the reason its not covering.

Your query considering 'workInprogress,Delivered,Closed won' as single word.
 
query==select id, stageName,AccountId, Account.Rating from opportunity where stageName IN ('workInprogress,Delivered,Closed won') AND AccountId !=Null

try with below belo class and test class you will get 100% .

Batch class:
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator start(Database.BatchableContext bc) {
     set<string> strlist = new set<string>{'workInprogress','Delivered','ClosedWon'};
      string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN :strlist AND AccountId !=Null';
     system.debug('query=='+query);
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
      
        for(opportunity o :Scope){
            o.Account.Rating='client';  
          system.debug('Rating'+o.Account.Rating);
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope;}
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];  
    }   
     }

Test Class:
 
@isTest
public class UpdateAccountRatingTest {
    @isTest
    public static void unit_test(){
       //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.Rating='prospect';
        insert acc;
        
        //insert All mandatory fields
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Accountid = acc.Id;
        opp.stageName = 'workInprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
         
       
        
        Test.startTest();
      
        UpdateAccountRating uar  = new UpdateAccountRating();
        Database.executeBatch(uar);
      String sch = '0 0 0 1 * ? *';
      string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
        Test.stopTest();
       

 }
    }

If this helps, Please mark it as best answer.

thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

Is your batch class working as expected??

Thanks!!
suji srinivasansuji srinivasan
User-added image

yeah
AnkaiahAnkaiah (Salesforce Developers) 
Hi Suji,

your query was in correct in the bact apex that is the reason its not covering.

Your query considering 'workInprogress,Delivered,Closed won' as single word.
 
query==select id, stageName,AccountId, Account.Rating from opportunity where stageName IN ('workInprogress,Delivered,Closed won') AND AccountId !=Null

try with below belo class and test class you will get 100% .

Batch class:
global class UpdateAccountRating implements Database.Batchable<sObject>, Database.Stateful {
    global Integer recordsProcessed = 0;
 global  Database.QueryLocator start(Database.BatchableContext bc) {
     set<string> strlist = new set<string>{'workInprogress','Delivered','ClosedWon'};
      string query='select id, stageName,AccountId, Account.Rating from opportunity where stageName IN :strlist AND AccountId !=Null';
     system.debug('query=='+query);
     return Database.getQueryLocator(query); 
    }
     global Void execute(Database.BatchableContext bc, List<opportunity> Scope){
      
        for(opportunity o :Scope){
            o.Account.Rating='client';  
          system.debug('Rating'+o.Account.Rating);
            recordsProcessed = recordsProcessed + 1;
        }
         update Scope;}
         global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + 'records processed. Shazam!');
        AsyncApexJob job = [SELECT Id, Status,NumberOfErrors,JobItemsProcessed,TotalJobItems, CreatedBy.Email 
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];  
    }   
     }

Test Class:
 
@isTest
public class UpdateAccountRatingTest {
    @isTest
    public static void unit_test(){
       //insert All mandatory fields
        Account acc =new Account();
        acc.Name = 'test';
        acc.Rating='prospect';
        insert acc;
        
        //insert All mandatory fields
        Opportunity opp = new opportunity();
        opp.Name = 'test opp';
        opp.Accountid = acc.Id;
        opp.stageName = 'workInprogress';
        opp.CloseDate = system.today()+5;
        insert opp;
         
       
        
        Test.startTest();
      
        UpdateAccountRating uar  = new UpdateAccountRating();
        Database.executeBatch(uar);
      String sch = '0 0 0 1 * ? *';
      string JobID = system.schedule('BatchJob',sch, new UpdateAccountRatingSchedular());
        Test.stopTest();
       

 }
    }

If this helps, Please mark it as best answer.

thanks!!
This was selected as the best answer