Here's a use case I'm trying to figure out for while:
I run the following batch class to update records:
MyBatchClass batchInstance = new MyBatchClass ();
database.executeBatch(batchInstance ,1);
The above code always runs from Developer Console. This code needs a delay timer of 2 minutes because if it does updates an existing record, they might lockout. Can I do this without a Scheduler or without creating a custom object? I would need the last run apex batch job. I tried the below not so generic code:
AsyncApexJob aaj1 =[ SELECT Id, CreatedDate, CreatedById, JobType, ApexClassId, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors, CompletedDate, MethodName, ExtendedStatus, ParentJobId, LastProcessed, LastProcessedOffset FROM AsyncApexJob where CompletedDate !=null order by CompletedDate desc limit 1];
if(System.now().minute() - aaj1.CompletedDate.minute()>120 && aaj1.ApexClassId =='01p01e21321318digit')
{
MyBatchClass b = new MyBatchClass ();
database.executeBatch(b,1);
}
else{
System.debug('Batch already ran or in In progress status, Please wait for 120 seconds')
}
I couldn't proceed with the following because I would already have to execute the batch in order for me to get the batch job Id (batchprocessid )
>Id batchprocessid = Database.executeBatch(new MyBatchClass, 200);
>AsyncApexJob aaj = [SELECT Id, CreatedDate, CreatedById, JobType, ApexClassId, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors, CompletedDate, MethodName, ExtendedStatus, ParentJobId, LastProcessed, LastProcessedOffset FROM AsyncApexJob WHERE ID =: batchprocessid ];
Is there any better way to do it? I didn't run the above code as it's on sensitive data and didn't want to take any chance. Any suggestion or guidance would be appreciated.
Thanks in advance!
SELECT...FOR UPDATE
might be one.