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
Sunny SSunny S 

Test Class Error Help!!

Hello dear community!!
Hoping somebody could help me with the below Test Class error.
 
I have a @isTest class which is failing the 'Run Test' throwing the following error message: '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.'
 
Apparently the Code Coverage is coming as 100%
 
I've referenced the following Salesforce article : https://help.salesforce.com/s/articleView?id=000330685&type=1
 
Test Class Code: 
 
@isTest private class MilestoneTest {         static testMethod void TestCompleteMilestoneCase(){         Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};         List<Account> acts = new List<Account>();         Account myAcc = new Account(Name='TestAct', phone='1001231234');         acts.add(myAcc);                  Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');         acts.add(busAcc);         insert acts;         Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id);         insert(cont);                  Id contactId = cont.Id;         Entitlement entl = new Entitlement();         List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE IsActive = true limit 1];         if (lstEntitlementProcess.size()>0){             entl = new Entitlement(Name='Customer Feedback SLA',SlaProcessId= lstEntitlementProcess[0].id,AccountId=busAcc.Id,type='Change of Status',                                     StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));             insert entl;         }                 String entlId;         if (entl != null)             entlId = entl.Id;          List<Case> cases = new List<Case>{};             if (entlId != null){                 Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',                                   EntitlementId = entlId, ContactId = contactId,slaStartDate=system.now());                 cases.add(c);             }         if(cases.isEmpty()==false){             insert cases;             List<Id> caseIds = new List<Id>();             for (Case cL : cases){                 caseIds.add(cL.Id);             }             Test.startTest();             milestoneUtils.completeMilestone(caseIds, milestoneNames, System.now());             Test.stopTest();         }     } }
 
*******#####*********
 
FYI - There is a Batch Class in the system which is running for a different installed Application.
 
Help: Could somebody suggest me the changes that I should be making to this Test Class in order to make it run successfully ?
 
Thanks in advance!!
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sunny,

Refer the help article to fix the issue.
https://help.salesforce.com/s/articleView?id=000330685&type=1

check this also.
https://www.infallibletechie.com/2015/11/systemunexpectedexception-no-more-than.html#:~:text=If%20you%20face%20%E2%80%9CSystem.,not%20called%20more%20than%20once.

If this information helps, Please mark it as best answer.

Thanks!!
Sunny SSunny S
Hi @ankaiah many thanks for your reply. 
I've already reffered the suggested links and unfortunately could not find a solution to my problem. 

Could you suggest what line of the Test Class should I update to make it Run ?

Many Thanks!!
Sunny SSunny S
@isTest private class MilestoneTest {         static testMethod void TestCompleteMilestoneCase(){         Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};         List<Account> acts = new List<Account>();         Account myAcc = new Account(Name='TestAct', phone='1001231234');         acts.add(myAcc);                  Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');         acts.add(busAcc);         insert acts;         Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id);         insert(cont);                  Id contactId = cont.Id;         Entitlement entl = new Entitlement();         List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE IsActive = true limit 1];         if (lstEntitlementProcess.size()>0){             entl = new Entitlement(Name='Customer Feedback SLA',SlaProcessId= lstEntitlementProcess[0].id,AccountId=busAcc.Id,type='Change of Status',                                     StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));             insert entl;         }                 String entlId;         if (entl != null)             entlId = entl.Id;          List<Case> cases = new List<Case>{};             if (entlId != null){                 Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',                                   EntitlementId = entlId, ContactId = contactId,slaStartDate=system.now());                 cases.add(c);             }         if(cases.isEmpty()==false){             insert cases;             List<Id> caseIds = new List<Id>();             for (Case cL : cases){                 caseIds.add(cL.Id);             }             Test.startTest();             milestoneUtils.completeMilestone(caseIds, milestoneNames, System.now());             Test.stopTest();         }     } }

 
AnkaiahAnkaiah (Salesforce Developers) 
Can you check milestoneUtils  class is calling from any batch class or not. If it is calling then check the batch size.

Thanks!!
Sunny SSunny S
Thanks again Ankaiah,

MilestoneUtil Class:
 
public class MilestoneUtils {
    public static void completeMilestone(List<Id> caseIds, 
            Set<String> milestoneName, DateTime complDate) {              
    List<CaseMilestone> cmsToUpdate = [select Id, completionDate
            from CaseMilestone cm
            where caseId in :caseIds and cm.MilestoneType.Name in: milestoneName 
            and completionDate = null];
          
    System.debug('cmsToUpdate==>'+cmsToUpdate); //new line added on 4th Aug22
    if (cmsToUpdate.isEmpty() == false){
        for (CaseMilestone cm : cmsToUpdate){
            cm.completionDate = complDate;
            }
        update cmsToUpdate;
        }
    }
}

 
Sunny SSunny S
There is also a Trigger which is referencing MilestoneUtils class.
 
trigger CloseOpenMilestone on Case (before insert,before update) {
    Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};
        if (UserInfo.getUserType() == 'Standard'){
            DateTime completionDate = System.now(); 
            List<Id> updateCases = new List<Id>();
            Id centralisedFeedbackRecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Centralised Customer Feedback 1.1').getRecordTypeId();
            for (Case c : Trigger.new){
                System.debug('c.isClosed =>'+c.isClosed+',  c.Status=>'+c.Status+',  c.SlaStartDate=>'+c.SlaStartDate + ', completionDate=>'+completionDate+
                             ', c.SlaExitDate=>'+c.SlaExitDate+',  c.RecordType=>'+c.RecordTypeId +', centralisedFeedbackRecordTypeId=>'+centralisedFeedbackRecordTypeId);
                if ((c.isClosed == true|| c.Status == 'Resolved'|| c.Status == 'Unresolved'|| c.Status =='Cancelled (Spam / Not Valid)') && c.SlaStartDate <= completionDate && c.SlaExitDate == null && c.RecordTypeId == centralisedFeedbackRecordTypeId)
                    updateCases.add(c.Id); 
            }
            System.debug('updateCases==>'+updateCases);
            if (updateCases.isEmpty() == false)
                milestoneUtils.completeMilestone(updateCases,milestoneNames, completionDate);
        }
}

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sunny,

try with below code.
@isTest private class MilestoneTest {         
static testMethod void TestCompleteMilestoneCase(){         
Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};        
 List<Account> acts = new List<Account>();         
 Account myAcc = new Account(Name='TestAct', phone='1001231234');         
 acts.add(myAcc);                  
 Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');        
 acts.add(busAcc);         
 insert acts;         
 Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id);         
 insert cont;                  
      
 Entitlement entl = new Entitlement();         
 List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE IsActive = true limit 1];                     
 entl = new Entitlement(Name='Customer Feedback SLA',SlaProcessId= lstEntitlementProcess[0].id,AccountId=busAcc.Id,type='Change of Status',                                     
 StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));             
 insert entl;                      
          
 List<Case> cases = new List<Case>();             
                
 Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',EntitlementId = entlId, 
 ContactId = contactId,slaStartDate=system.now());                 
 cases.add(c);                            
 insert cases;                       
  
 }
 }

If this helps, Please mark it as best answer.

Thanks!!
Sunny SSunny S

Thanks again Ankaiah. 

Now getting the following Error 'Error: Compile Error: Variable does not exist: entlId at line 21 column 93' in line :   
Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',EntitlementId = entlId, 

AnkaiahAnkaiah (Salesforce Developers) 
Hi Sunny,

try with below code.
 
@isTest private class MilestoneTest {         
static testMethod void TestCompleteMilestoneCase(){         
Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};        
 List<Account> acts = new List<Account>();         
 Account myAcc = new Account(Name='TestAct', phone='1001231234');         
 acts.add(myAcc);                  
 Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');        
 acts.add(busAcc);         
 insert acts;         
 Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id);         
 insert cont;                  
      
 Entitlement entl = new Entitlement();         
 List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE IsActive = true limit 1];                     
 entl = new Entitlement(Name='Customer Feedback SLA',SlaProcessId= lstEntitlementProcess[0].id,AccountId=busAcc.Id,type='Change of Status',                                     
 StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));             
 insert entl;                      
          
 List<Case> cases = new List<Case>();             
                
 Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',EntitlementId = entl.id, 
 ContactId = contactId,slaStartDate=system.now());                 
 cases.add(c);                            
 insert cases;                       
  
 }
 }
If this helps, Please mark it as best answer.

Thanks!!
 
Sunny SSunny S
Sorry mate, another error with this line:  ContactId = contactId,slaStartDate=system.now()); 

Error message: 
Error: Compile Error: Variable does not exist: contactId at line 23 column 14
AnkaiahAnkaiah (Salesforce Developers) 
try with below.

you need to assign the value for contactId = cont.id
 
@isTest private class MilestoneTest {         
static testMethod void TestCompleteMilestoneCase(){         
Set<String> milestoneNames = new Set<String>{'Resolution Time','Waiting for Customer','Change of Status'};        
 List<Account> acts = new List<Account>();         
 Account myAcc = new Account(Name='TestAct', phone='1001231234');         
 acts.add(myAcc);                  
 Account busAcc = new Account(Name = 'TestForMS', phone='4567890999');        
 acts.add(busAcc);         
 insert acts;         
 Contact cont = new Contact(FirstName = 'Test', LastName = 'LastName', phone='4567890999', accountid = busAcc.id);         
 insert cont;                  
      
 Entitlement entl = new Entitlement();         
 List<SlaProcess> lstEntitlementProcess = [SELECT Id, Name FROM SlaProcess WHERE IsActive = true limit 1];                     
 entl = new Entitlement(Name='Customer Feedback SLA',SlaProcessId= lstEntitlementProcess[0].id,AccountId=busAcc.Id,type='Change of Status',                                     
 StartDate=Date.valueof(System.now().addDays(-2)), EndDate=Date.valueof(System.now().addYears(2)));             
 insert entl;                      
          
 List<Case> cases = new List<Case>();             
                
 Case c = new Case(Subject = 'Test Case with Entitlement ', Priority='High',EntitlementId = entl.id, 
 ContactId = cont.id,slaStartDate=system.now());                 
 cases.add(c);                            
 insert cases;                       
  
 }
 }
If this helps, Please mark it as best answer.
Thanks!!
Sunny SSunny S
Thank you again Anakaiah. 
I am not able to save the Test Class but still getting the same Errror upon doing a Run Test.


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.

Not sure how do I overcome this error.
Sunny SSunny S
Thank you again Anakaiah. 
I am able to save the Test Class but still getting the same Errror upon doing a Run Test.


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.

Not sure how do I overcome this error.
AnkaiahAnkaiah (Salesforce Developers) 
FYI - There is a Batch Class in the system which is running for a different installed Application.

Can you check that batch class, which objects records its processing?

refer the below link might help you to fix the issue.

https://salesforce.stackexchange.com/questions/174732/no-more-than-one-executebatch-can-be-called-from-within-a-test-method

Thanks!!