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
Jagadeesh AngadiJagadeesh Angadi 

Please clarify my below doubt on batch execution sequence.

I have batch class, basically it will query on account where Account_Status__c is empty(Account_Status__c='') then it will create contact for such accounts then Account.Account_Status__c will be updated to 'Contact Generated' so that same account will not be picked for next batch processing. This batch class is scheduled to run for every 5 minutes.
Now consider the example - First batch execution is started @ 10AM but it is not finished within 10.05AM(because number of batches to be processed is more hence it is taking much time to finish), then at 10.05 AM next set of batch processing will start. During second batch execution will system consider same records as in first execution(because first batch is not yet completed and Account_Status__c field is not yet updated on account). If yes, then it will create duplicate contacts for an account. Please clarify how to handle this issue? Please note that, I cannot increase the time interval between batch execution because of some reasons.
ShirishaShirisha (Salesforce Developers) 
Hi Jagadeesh,

Greetings!

I would suggest you to put the interval more than 5 mins as the best practice.Also,the Batch is an asynchronous process so we can't estinate the ETA as it depends on the resource availability.

If you are unable to put the interval then you can actually create the duplicate rule on the Contact which will allow the batch to create the duplicate records.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
David Zhu 🔥David Zhu 🔥

I would suggest you create a scheduleable class, in the schedulable class, execute the batch class.

In FINISH method of the batch class, call the scheduleable class run at 5 minutes from now.

That will prevent multiple executions.

Before scheduling the next job, you may need to abort the job with the same schedule job name.

Anmol AgarwalAnmol Agarwal
Try below solutions:
  • Kick off your 1st batch from Anonymous code and in the finish call the next occurance of the same batch so that the same records will never be picked
  • While fetching records on Account put FOR UPDATE so that the record will be locked and will not be updated by another batch