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
DritterDritter 

System.UnexpectedException: No more than one executeBatch can be called from within a testmethod

My test class keeps getting this error when I push to production. I found the following article (https://help.salesforce.com/apex/HTViewSolution?id=000176562&language=en_US) and added the code to lines at the bottom. However, I keep getting a Compile Error: unexpected token: ')' at line 49 column 51. 

Is this the best way to resolve the original error? If so, any ideas on why I'm getting the Compile Error and how to overcome it? I've tried a few things but they all result in the same error. 

/************************************************************************************
** Module Name   : UserCleanUp 
**
** Description   : This class deactivates portal users setup by the website and
** irostering. It also deactivates Salesforce users that haven't logged in in the 
** last 90 days with the exception of Guest Site Users.
**
** Technial Info : <Batch Apex Class>
** 
** Author     : 
** Revision History:-
** Version  Date        Author 
** 1.0      1/6/15      
************************************************************************************/

global class KapUserCleanUp implements Database.Batchable<sObject>{

    datetime myDateTime = datetime.now();
    datetime newDateTime = myDateTime.addHours(-3);
    datetime inactiveDateTime = myDateTime.addDays(-90);

    //System.debug('current time: ' + myDateTime);
    //System.debug('newDateTime time: ' + newDateTime);
    //System.debug('inactiveDateTime time: ' + inactiveDateTime);

global Database.querylocator start(Database.BatchableContext BC){
            return Database.getQueryLocator([SELECT Email,Id,LastLoginDate FROM User WHERE (IsPortalEnabled = True AND IsActive = true AND (lastLoginDate < :newDateTime OR lastLoginDate = null)) OR (IsPortalEnabled = false AND isActive = true AND lastLoginDate < :inactiveDateTime AND LastName != 'Site Guest User')]);}

global void execute(Database.BatchableContext BC, List<User> users){
   System.debug('Users retrieved ' + Users);
   
   for (User u : users) {
       u.isActive = false;
       System.debug(u.id + ' ' + u.email + ' will be deactivated' + u.lastLoginDate);
   }
       Database.SaveResult[] srList = Database.update(users, false);
          for (Database.SaveResult sr : srList){
             if (!sr.isSuccess()) {
             //Operation failed, so get all errors
                for(Database.Error err : sr.getErrors()){
                     System.debug('The following error has occurred.');
                     System.debug(err.getStatusCode() + ': ' + err.getMessage());
                } 
             }  
          }
    }
   global void finish(Database.BatchableContext BC){
    if(!Test.isRunningTest)
         Database.executeBatch(new MySecondBatchJob));
       }
   }
Best Answer chosen by Dritter
bob_buzzardbob_buzzard
That code belongs in your main class, as the main class will skip it when running in a production context. 

Looking again, the issue isn't that there is a bonus closing bracket, rather missing an opening bracket.  The new operator invokes the MySecondBatchJob constructor, which needs to be followed by pair of brackets:
 
Database.executeBatch(new MySecondBatchJob());



 

All Answers

bob_buzzardbob_buzzard
Line 49 has a bonus closing bracket at the end of it:
 
Database.executeBatch(new MySecondBatchJob));
You only open a single bracket as part of the executeBatch function invocation, so you only need to close a single one:
 
Database.executeBatch(new MySecondBatchJob);
bob_buzzardbob_buzzard
The code snippet in the knowledgebase is wrong too, as that has the bonus bracket also.
DritterDritter
I removed that extra bracket, but it produces the same error. I'm not sure why
bob_buzzardbob_buzzard
Can you clarify what "the same error" means - is that the syntax error or the multiple execute batch error?
DritterDritter
Hi Bob,

I removed the bonus bracket, but when I click save it still produces the syntax error:

Compile Error: unexpected token: ')' at line 49 column 51

I have been getting conflicting advice from other developers. Does the code belong in the main class (picture above) or in my test class? 
 
if(!Test.isRunningTest)
     Database.executeBatch(new MySecondBatchJob);

Thanks for the help!
bob_buzzardbob_buzzard
That code belongs in your main class, as the main class will skip it when running in a production context. 

Looking again, the issue isn't that there is a bonus closing bracket, rather missing an opening bracket.  The new operator invokes the MySecondBatchJob constructor, which needs to be followed by pair of brackets:
 
Database.executeBatch(new MySecondBatchJob());



 
This was selected as the best answer