Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void addBatchIfNotRunning(Id apexBatchClassId){
- ApexAsyncJob asyncJobList = [Select Id From ApexAsyncJob where ApexClassId=:apexBatchClassId AND JobType='BatchApex' and Status In ('Processing','Preparing','Holding','Queued')];
- boolean isNeedToInitiateNewBatch = true;
- if(!asyncJobList.isEmpty()){
- for(ApexAsyncJob as : asyncJobList){
- if(as.Status == ''Processing' || as.Status == 'Preparing'){ //As start method of batch is running or run, it wont pick new records so abort it
- System.abortJob(as.Id);
- }else{
- //Batch is already queued or in Holding stage so it will pick up new records
- isNeedToInitiateNewBatch = false;
- }
- }
- }
- if(isNeedToInitiateNewBatch){
- //Initiate Batch
- }
- }
Add Comment
Please, Sign In to add comment