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
Sathish LoganathanSathish Loganathan 

Batch Class is not Covered even 1 %

Hi Below is my Batch Class , Scheduler Class and Test Class for it. I don't know  what's wrong in the code, My class is not getting covered even 1 %. 

Please help

Batch Class:
-----------------
global class accountSalesUpdate implements Database.Batchable<sObject> {
    
    global final String query;
    
     global accountSalesUpdate( String q){
         query =q;
       //q='SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
         
    }   

global Database.QueryLocator start(Database.BatchableContext BC)
    {
        //String query = 'SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
        return Database.getQueryLocator(query);
    }   
   
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
         Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('HE').getRecordTypeId();
         for(Account a : scope)
         {
             if(a.ParentId!=NULL && a.AccountStatusUpdate__c==False && a.RecordTypeId==recordTypeId){
             Account pid=[Select id,AccountStatusUpdate__c from Account where id=:a.ParentId];
                 List<Account> acc= new List<Account> ([Select id ,name, Account_Status_Technology__c,AccountStatusUpdate__c,RecordTypeId,Sales_Team__c,ParentId from Account where ParentId=:pid.id ]);
                 List<Account> accs1 = new List<Account>();
    for(Account ac1:acc){
        accs1.add(ac1); 
    } 
                
                for(Account accounts:accs1){
                     if(accounts.Account_Status_Technology__c=='Active'){
                         accounts.Sales_Team__c='Account Management';
                         pid.AccountStatusUpdate__c=True;
                          pid.Sales_Team__c='Account Management';
                          update pid;
                     }
                    else{
                        accounts.Sales_Team__c='Account Management';
                    }
                    if(accounts.Account_Status_Technology__c=='Inactive' && pid.AccountStatusUpdate__c==False){
                         accounts.Sales_Team__c='New Business';
                        pid.Sales_Team__c='New Business';
                        update pid;
                     }
                    
                 } 
                 update accs1;
                 }
                 }
                 
        
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

Test Class:
-----------------------

@isTest
public class AccountSalesUpdateTest {
    
    public Static testmethod void accountUpdate(){
        Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('HE').getRecordTypeId();
        Account parent = New Account(Name ='Parent Account', RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert parent;
        Account child1= new Account(Name='Child1',ParentId= parent.id,Account_Status_Technology__c='Active',RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert child1;
        Account child2= new Account(Name='Child2',ParentId= parent.id,Account_Status_Technology__c='Inactive',RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert child2;
        Test.startTest();
       // String query = 'SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
        accountSalesUpdate c = new accountSalesUpdate('SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account');
       Database.executeBatch(c);
        test.stopTest();
        
    } 
    
}

Scheduler Class:
-------------------------
global class accountSalesUpdateScheduler implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    String query = 'SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
    accountSalesUpdate b = new accountSalesUpdate(query); 
      database.executebatch(b);
    }

 
ManojjenaManojjena
Hi,
Is your batch class working as expected ???
3 Creeks3 Creeks
Try this:
.....

accountSalesUpdate aSUpdate = new accountSalesUpdate();
String query = 'SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
aSUpdate.query = query;
Id bId = Database.executeBatch(aSUpdate);

 
Abhishek BansalAbhishek Bansal
Hi Satish,
I refactored your all three classes:
Please replace the code of your classes with below code :

Batch Class:
global class accountSalesUpdate implements Database.Batchable<sObject> {
    
    global final String query;

    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,ParentId,AccountStatusUpdate__c,RecordTypeId FROM Account';
        return Database.getQueryLocator(query);
    }   
   
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {
         Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('HE').getRecordTypeId();
         for(Account a : scope)
         {
             if(a.ParentId!=NULL && a.AccountStatusUpdate__c==False && a.RecordTypeId==recordTypeId){
            	 Account pid=[Select id,AccountStatusUpdate__c from Account where id=:a.ParentId];
                 List<Account> acc= new List<Account> ([Select id ,name, Account_Status_Technology__c,AccountStatusUpdate__c,RecordTypeId,Sales_Team__c,ParentId from Account where ParentId=:pid.id ]);
                 List<Account> accs1 = new List<Account>();
                 for(Account ac1:acc){
                	 accs1.add(ac1); 
                 } 
                
                 for(Account accounts:accs1){
                     if(accounts.Account_Status_Technology__c=='Active'){
                         accounts.Sales_Team__c='Account Management';
                         pid.AccountStatusUpdate__c=True;
                         pid.Sales_Team__c='Account Management';
                         update pid;
                     }
                     else{
                        accounts.Sales_Team__c='Account Management';
                     }
                     if(accounts.Account_Status_Technology__c=='Inactive' && pid.AccountStatusUpdate__c==False){
                         accounts.Sales_Team__c='New Business';
                         pid.Sales_Team__c='New Business';
                         update pid;
                     }
                    
             	} 
                update accs1;
         	}
     	}
	}   
    global void finish(Database.BatchableContext BC){
    }
}

Scheduler Class :
global class accountSalesUpdateScheduler implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    	accountSalesUpdate b = new accountSalesUpdate(); 
    	database.executebatch(b);
    }
}

Test class :
 
@isTest
public class AccountSalesUpdateTest {
    
    public Static testmethod void accountUpdate(){
        Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('HE').getRecordTypeId();
        Account parent = New Account(Name ='Parent Account', RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert parent;
        Account child1= new Account(Name='Child1',ParentId= parent.id,Account_Status_Technology__c='Active',RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert child1;
        Account child2= new Account(Name='Child2',ParentId= parent.id,Account_Status_Technology__c='Inactive',RecordTypeId=recordTypeId,BillingStreet='Test',BillingCity='Test',BillingState='Test',BillingCountry='Test',BillingPostalCode='22311');
        insert child2;
        Test.startTest();
        String CRON_EXP = '0 0 0 15 3 ? 2022';
        System.schedule('ScheduleApexClassTest',CRON_EXP, new accountSalesUpdateScheduler());
        test.stopTest();
    } 
}

Please update your classes and then run your test class.
Let me know if you need more help or if you have any issues.

Thanks,
Abhishek
Sathish LoganathanSathish Loganathan
@Manoj : Yes logic all working fine, Even If I schedule the class its working fine.
Sathish LoganathanSathish Loganathan
@3Creeks:
    I tried that too, but no progress.
ManojjenaManojjena
Hi,

Have u commented the query part before posting to forum ?
 
Sathish LoganathanSathish Loganathan
Hi All,
   I raised a case with SF and they resolved it, My class got coverage after Salesforce asked me to compile all classes in SF.

Thanks all.