Guest User

Untitled

a guest
Dec 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. public static void addBatchIfNotRunning(Id apexBatchClassId){
  2.  
  3. ApexAsyncJob asyncJobList = [Select Id From ApexAsyncJob where ApexClassId=:apexBatchClassId AND JobType='BatchApex' and Status In ('Processing','Preparing','Holding','Queued')];
  4. boolean isNeedToInitiateNewBatch = true;
  5. if(!asyncJobList.isEmpty()){
  6. for(ApexAsyncJob as : asyncJobList){
  7. if(as.Status == ''Processing' || as.Status == 'Preparing'){ //As start method of batch is running or run, it wont pick new records so abort it
  8. System.abortJob(as.Id);
  9.  
  10. }else{
  11. //Batch is already queued or in Holding stage so it will pick up new records
  12. isNeedToInitiateNewBatch = false;
  13. }
  14.  
  15. }
  16. }
  17. if(isNeedToInitiateNewBatch){
  18. //Initiate Batch
  19. }
  20.  
  21. }
Add Comment
Please, Sign In to add comment