You need to sign in to do that
Don't have an account?
Sathish 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);
}
}
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);
}
}
Is your batch class working as expected ???
Please replace the code of your classes with below code :
Batch Class:
Scheduler Class :
Test class :
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
I tried that too, but no progress.
Have u commented the query part before posting to forum ?
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.