1

I have batch class, basically it will query on account records where Account_Status__c is empty(Account_Status__c='') then it will create contact for such accounts then Account.Account_Status__c field 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 again batch class will start its second execution as per schedule. 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.

2

The batches are generated at the beginning (during the start method). As such, each subsequent batch would query any records that haven't yet updated. The solution is to re-query the accounts in the execute method to see if they still require the additional logic to be executed:

public void execute(Database.BatchableContext context, Account[] scope) {
  scope = [SELECT Id,... FROM Account WHERE Account_Status__c = null and Id = :scope];
  // Rest of logic //
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.