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
Kriti KhannaKriti Khanna 

Synchronous calling in apex batch

There is a main apex program calling batch apex for insertion. While batch insertion is still in progress, Next statements of Main programs are executed.
I want to execute the next statements only when apex batch processing is complete. Any way to do it?

I tried setting a flag in finish function of the batch apex and added wait in the Main program till the flag is set. But it gives CPU time limit exceeded error. Any way to bypass this issue? 
v varaprasadv varaprasad
Hi Kriti,

Try like below:
global void finish(Database.BatchableContext BC)
    {
        AsyncApexJob a = [Select Id, Status,ExtendedStatus,NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Email    from AsyncApexJob where Id =:BC.getJobId()];
       if(a.status = 'Completed'){
	       //Call your class
		   
	   }else{
	       system.debug('Batch is not completed yet'+a.status);
	   }
        
    }


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
v varaprasadv varaprasad
if(a.status == 'Completed'){
Raj VakatiRaj Vakati
if(a.status == 'Completed'){ will not work .. because you are still at same batch or may not complete your finish method  .. 

What you can do it is ...


Call the apex batch using some time delay by using scheduler ..