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
vaishali sharmavaishali sharma 

Calling execute inside finish method of batch apex.

hi , i want to call excute method inside finish method of batch class . How can i achieve this . When i try to do this it throws error 
/* Error: Compile Error: expecting a right parentheses, found 'BC' at line 100 column 58 */

     
    Global void finish(Database.BatchableContext BC){
      system.debug('finish method has beenn executed');
      if (flag == false)
         {
           BatchDuplicateRecord  batchDupreco = new BatchDuplicateRecord();
           batchDupreco.execute(Database.BatchableContext BC,List<sObject> scope);
            Database.executeBatch(batchDupreco,1000);
         }
       else
       {
          system.debug('your batch has succesfully executed');
       }
     }
    
James LoghryJames Loghry
Comment out this line and you should be good to go:
 
batchDupreco.execute(Database.BatchableContext BC,List<sObject> scope);

 
Prem Anandh 1Prem Anandh 1

Hi Vaishali,

I belive below line is not required for your requirement. 
 

batchDupreco.execute(Database.BatchableContext BC,List<sObject> scope);

Thanks, 
Prem Anandh
Pankaj_GanwaniPankaj_Ganwani
Hi Vaishali,

You don't need to call the execute method explicitly from the finish method of the batch apex. All you need to do is to call Database.executeBach() method from finish method.
 
Global void finish(Database.BatchableContext BC){
      system.debug('finish method has beenn executed');
      if (flag == false)
         {
            Database.executeBatch(new BatchDuplicateRecord  (),1000);
         }
       else
       {
          system.debug('your batch has succesfully executed');
       }
     }

 
DebasisDebasis
Hi Vaishali,

it is good practice to check how many batch are executing in same time, as we cant execute more than 5 jobs in same time we need to check the number of jobs executing and then start another job.
 So, I have queries AsyncApexJob object to check how many batch jobs are Queued,Processing,Preparing. if count is more than 4 then i am scheduling this batch to excute after 2 minue and in meanwhile other jobs will complete. if there are less than 5 jobs are processing in system then excute the batch now.
global void finish(Database.BatchableContext BC)
    {
        
        if (flag == false){
        Integer jobs = [Select count() From AsyncApexJob Where JobType = 'BatchApex' and ( Status = 'Queued' or Status = 'Processing' or Status = 'Preparing' )];
        if( jobs > 4 )
        {
           // try again in a minute
            

            BatchDuplicateRecord  SchdBatchDupreco = new BatchDuplicateRecord();
			
            System.scheduleBatch ( SchdBatchDupreco,'EmailInvoicesBatchSchedule ', 2 );  //execute batch job after 2 minute              
        }
        else
        {
            BatchDuplicateRecord  batchDupreco = new BatchDuplicateRecord();
            Database.executeBatch( batchDupreco, 1000 );//execute batch job now
        }
		}
		else{
		system.debug('your batch has succesfully executed');
		}
    }

 
vaishali sharmavaishali sharma
Thank you to all  for your reply but the logic that i need to write down is if my start method get failed or query of start method get failed even in that case execeute method should get executed for that i need to call execute method inside finish method.
DebasisDebasis
Hi Vaishali,

if you want to call execute method inside finish method, you need to pass batchablecontext value and list of record to execute. but in your current code you are not passing any value to it.
    
check below code for it, if any exception occur then call execute method with current batch context and scope as the list of record to proceed.
global void finish(Database.BatchableContext BC)
    {
        
        if (flag == false){
        Integer jobs = [Select count() From AsyncApexJob Where JobType = 'BatchApex' and ( Status = 'Queued' or Status = 'Processing' or Status = 'Preparing' )];
        if( jobs > 4 )
        {
           // try again in a minute
            

            BatchDuplicateRecord  SchdBatchDupreco = new BatchDuplicateRecord();
			try{
            System.scheduleBatch ( SchdBatchDupreco,'EmailInvoicesBatchSchedule ', 2 );  //execute batch job after 2 minute   
			}
			catch(exception ex){
				//assume scope is the list of record you want to process if exception occur
				batchDupreco.execute(BC,scope);
			}
			
        }
        else
        {
            BatchDuplicateRecord  batchDupreco = new BatchDuplicateRecord();
			try{
            Database.executeBatch( batchDupreco, 1000 );//execute batch job now
			}
			catch(exception ex){
				//assume scope is the list of record you want to process if exception occur
				batchDupreco.execute(BC,scope);
			}
        }
		}
		else{
		system.debug('your batch has succesfully executed');
		}
    }
please do let me know if it helps you.

Thanks,
Debasis
vaishali sharmavaishali sharma
Thanks Debasis . Your code help me bit.