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
Steve BerleySteve Berley 

No batch calls, but getting "No more than one executeBatch can be called from within a test method" error

Hi everyone,

I've got some code that's amazingly simple but is throwing the craziest error when I try to deploy. 
  • The code has 100% coverage in the sandbox with no errors
  • Even when deploying from a brand new sandbox I get the error.
  • There are no batch calls in the code
I'm at a loss becuase the error is so unrelated to the code.  

Thanks for your help...

Steve 

The error is:
System.UnexpectedException: No more than one executeBatch can be called from within a test method. Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.

The code follows...
trigger TaskTrigger on Task (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
 
    if (trigger.isAfter && trigger.isInsert) {
        TaskHandler.evaluateTasks(trigger.new);
    }
}
 
public with sharing class TaskHandler {
    // public TaskHandler() {  }
 
    public static void evaluateTasks(list<task> newTasks){
        set<string> didNums = new set<string>();
        set<id> leadIDs = new set<id>();
        for (task t : newTasks){
            if ( string.isNotBlank(t.Dialpad__PhoneNumber__c) ) {
                didNums.add( LeadSourceHandler.scrubber(t.Dialpad__PhoneNumber__c) );
            }
            if ( string.isNotBlank(t.WhoID) ) leadIDs.add(t.WhoID);
        }
        map<string, string> LeadSources = getLeadSources(didNums);
        set<id> LeadsInNeed = getLeadIDs(leadIDs);
       
        list<lead> leadsToUpdate = new list<lead>();
        list<Lead_Source__c> newLeadSources = new list<Lead_Source__c>();
        for (task t : newTasks){
            string didNum = LeadSourceHandler.scrubber(t.Dialpad__PhoneNumber__c);
 
            if ( LeadsInNeed.contains(t.WhoID) ){
                if ( LeadSources.containsKey( didNum ) ) {
                    leadsToUpdate.add( new Lead(id=t.WhoID, LeadSource = LeadSources.get( didNum ) ) );
                } else {
                    newLeadSources.add( new Lead_Source__c(name = t.Dialpad__PhoneNumber__c ) );
                }
            }
        }
 
        if( leadsToUpdate != null && leadsToUpdate.size() > 0 ) update leadsToUpdate;
        if( newLeadSources != null && newLeadSources.size() > 0 ) insert newLeadSources; // save new ones
    }
 
    public static set<id> getLeadIDs(set<id> leadIDs){
        map<id, lead> leadsInNeed = new map<id, lead>([select id from lead where LeadSource = null and id in :leadIDs]);
        return leadsInNeed.keySet();
    }
 
   
    public static map<string, string> getLeadSources(set<string> didNums){
        map<string, string> m = new map<string, string>();
        for (Lead_Source__c ls : [select name, lead_source__c from Lead_Source__c where name in :didNums and Lead_Source__c != null]){
            m.put(ls.name, ls.Lead_Source__c);
        }
        return m;
    }
}
 
@isTest
private class TaskHandler_test {
    // private TaskHandler_test() {  }
 
    @isTest static void AssignLeadSourcesPrimary_test(){
        insert new Lead_Source__c(name='1234', lead_source__c='source 1');
 
        lead l1 = new lead(lastname='lead 1', Language__c='English', Location__c='Los Angeles');
        lead l2 = new lead(lastname='lead 2', Language__c='English', Location__c='Los Angeles');
        insert new list<lead>{l1, l2};
 
        task t1 = new task(subject='task 1', whoID=l1.id, activitydate=date.today(), Dialpad__PhoneNumber__c='1234');
        task t2 = new task(subject='task 2', whoID=l2.id, activitydate=date.today(), Dialpad__PhoneNumber__c='47');
        insert new list<task>{t1, t2};
    }
 
}


 
Best Answer chosen by Steve Berley
SwethaSwetha (Salesforce Developers) 
HI Steve,
This seems to be odd that without having a batch class this error is thrown. Can you setup debug logs (like mentioned in https://salesforce.stackexchange.com/questions/101988/debug-log-of-deployment-test-execution)and see if there stack trace of the executeBatch execution? 

Thanks

All Answers

SwethaSwetha (Salesforce Developers) 
HI Steve,
This seems to be odd that without having a batch class this error is thrown. Can you setup debug logs (like mentioned in https://salesforce.stackexchange.com/questions/101988/debug-log-of-deployment-test-execution)and see if there stack trace of the executeBatch execution? 

Thanks
This was selected as the best answer
Steve BerleySteve Berley
Wow!  I never knew you could do that!  Thanks!

While it gave me some hints of what's going on; in the end a bunch of just monkeying around made it work.
SwethaSwetha (Salesforce Developers) 
Glad to know it works now. Please consider marking the answer best to close the thread. Thank you