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
TanyrilTanyril 

Need some help: No more than one executeBatch can be called from within a test method

I'm having some trouble with my code and I can't figure out what's wrong. I've used identical syntax in other classes but for some reason this one isn't working.

 

The objective of this class is to read the valuation date, determine if it's less than 30 days old, then act based on that result (changing the value of a field to 1 if it is < 30 days old or set to 0 if > 30)

 

global with sharing class CAB_ThisMonthBoolean implements Database.Batchable<sObject>{
   global final String Query;

   global CAB_ThisMonthBoolean() {
        this.Query = 'Select cab.navmfv2__Valuation_Date__c, cab.Id, cab.ThisMonthBoolean__c, cab.ThisMonth__C from navmfv2__Client_Account_Balance__c cab ';
   }
   
   global Database.QueryLocator start(Database.BatchableContext BC) {
      return Database.getQueryLocator(Query);
   }

   global void execute(Database.BatchableContext BC, List<sObject> scope) {


    List<navmfv2__Client_Account_Balance__c> ClientAccountbalTrans = (List<navmfv2__Client_Account_Balance__c>) scope;
    List<navmfv2__Client_Account_Balance__c> updateClientAccountbal = new List<navmfv2__Client_Account_Balance__c>();
    
    for (navmfv2__Client_Account_Balance__c ClientAccountbalTran : ClientAccountbalTrans) {
        
        date currentDate = Date.today();
        date val = ClientAccountbalTran.navmfv2__Valuation_Date__c;
	    If(val.daysBetween(currentDate) < 30) {
			ClientAccountbalTran.ThisMonthBoolean__c = 1;
	    }
		else { ClientAccountbalTran.ThisMonthBoolean__c = 0; }
		updateClientAccountbal.add(ClientAccountbalTran);
        }
    update updateClientAccountbal;

    
}
    

   global void finish(Database.BatchableContext BC){

   }
   
 
public static testMethod void testBatch() {

 Account a = new Account(Name = 'Test Account', ShippingPostalCode = '00000', BillingCountry = 'US');
    insert a;

Contact con = new Contact(LastName = 'BatchTestContact');  
 insert con;

navmfv2__Fund__c Fund = new navmfv2__Fund__c(Name = 'Fake Fund', navmfv2__Fund_Short_Name__c = 'Fake Fund', navmfv2__CUSIP__c = '12345');
insert Fund;

navmfv2__Client_Account__c tstCA = new navmfv2__Client_Account__c (navmfv2__Client_Account_Number__c = '123TestCANum123', navmfv2__Sales_Rep_Name__c = con.id);
        insert tstCA;

Date thedate = Date.newInstance(2010,5,23);
navmfv2__Client_Account_Balance__c tstCAB = new navmfv2__Client_Account_Balance__c (navmfv2__Client_Account_Id__c = tstCA.Id, navmfv2__Valuation_Date__c = thedate, ThisMonthBoolean__c = 1);
        insert tstCAB;
       

    Test.StartTest();
    CAB_ThisMonthBoolean BatchClass = new CAB_ThisMonthBoolean();
    Database.executeBatch(BatchClass, 1);
    Test.StopTest();
  }
}

 

The code works in my sandbox (IE- It does what it's supposed to)- however I can't run it's test. 

Best Answer chosen by Admin (Salesforce Developers) 
TanyrilTanyril

Well- I had accidentally set the API version to 23... Fixed that and solved my own problems.