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
Clement Debrueres-BartoliClement Debrueres-Bartoli 

How to delete Cases owned by specific queue 4 hours after Case Closure?

Hello fellas,

For my spam emails, I currently have this process for Cases:

-if Custom field "Mark as Spam" is ticked by User, then move the Case to "Spam" queue and mark the Status as "Closed"

This PB works well, no problem. All my Spams go to this queue, in Closed status.

However I would also like to integrate the deletion of those Cases in the process: Case should be deleted 4 hours after they have been moved and closed. (I dont want to delete immediately, I need the Case to stay a few hours in the queue)

There are several options, but which one would be the simplest and cleanest to achieve this regular deletion of spam cases?

Thank you very much
Best Answer chosen by Clement Debrueres-Bartoli
Raj VakatiRaj Vakati
You have to use the apex batch .. 

 
Global class BatchMassDeleteRecs Implements Database.batchable<sobject>{
      
     global Database.QueryLocator start(Database.BatchableContext BC){

      return Database.getQueryLocator([YOUR_SOQL_QUERY]);
     }
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
         delete scope;
    
    }
    global void finish(Database.BatchableContext BC){
    }

 }

 

All Answers

Raj VakatiRaj Vakati
You have to use the apex batch .. 

 
Global class BatchMassDeleteRecs Implements Database.batchable<sobject>{
      
     global Database.QueryLocator start(Database.BatchableContext BC){

      return Database.getQueryLocator([YOUR_SOQL_QUERY]);
     }
     global  void execute(Database.BatchableContext BC,List<SObject> scope){
         delete scope;
    
    }
    global void finish(Database.BatchableContext BC){
    }

 }

 
This was selected as the best answer
JayantJayant
An scheduled class should be able to do it. 
Raj VakatiRaj Vakati
call the batch from the scheduler and that is the safe way .and you dnt end up with salesforce limits 
Clement Debrueres-BartoliClement Debrueres-Bartoli
Hey guys, thanks a lot for your suggestions I have done it like that:
______________________________________________
global class DeleteSpamCases implements Database.Batchable<sObject>,System.Schedulable{

   global final String Query;
  
   global Database.QueryLocator start(Database.BatchableContext BC){
      Date dToday = Date.today();
      Date t = Date.newInstance(dToday.Year() , dToday.Month() , dToday.Day() );
      return Database.getQueryLocator([Select id from Case where Owner.name = 'SpamBox']);
   }
   global void execute(Database.BatchableContext BC, List<sObject> scope){
     List<Case> lCase = new List<Case>();
     for(sobject s : scope){
     lCase.add((Case) s);
     }
     delete lCase;
    }

   global void finish(Database.BatchableContext BC){
   }
   
   public void execute(SchedulableContext SC){
    Database.executeBatch(new DeleteSpamCases(), 20);
  }

}