5

Is there a way to schedule the job so that it deletes cases that are 30 days old in that queue?

Right now it will delete everything on the last day of the month as designed but what I would like is for it to delete everything that is 30 days old and leave everything that is less than 30 days.

Please assist me in creating a query:

Group junkQueue = [select Id from Group where DeveloperName = 'Junk' and Type = 'Queue' Limit 1]; 
if(junkQueue.Id != null)
{ 
 String query= 'Select id from Case Where OwnerId = \''+junkQueue.Id+'\'';
 BatchDeleteCTSBQueueCases delCases = new BatchDeleteCTSBQueueCases(query);  
 Database.executebatch(delCases,100); 
}
  • In Batch jobs I like to query all the records out and don't put qualifiers in a query, I let my execute method figure out all the qualifying logic. This allows your batch job for Groups to be generic and reusable.. Something to think about – EricSSH Jan 7 '16 at 21:31
6
Group junkQueue = [select Id from Group where DeveloperName = 'Junk' and Type = 'Queue' Limit 1]; 
String query= 'Select id from Case Where OwnerId = \''+junkQueue.Id+'\' AND ClosedDate < LAST_N_DAYS:30'; BatchDeleteCTSBQueueCases delCases = new BatchDeleteCTSBQueueCases(query); Database.executebatch(delCases,100); 

You can add a dynamic filter using LAST_N_DAYS.

Note that if your query failed, you'd get a QueryException (Query has no rows for assignment), so it's not necessary to check if the ID is null (it is guaranteed not to be).

Edit: If you want open cases, you can use CreatedDate, or cases that haven't been touched in thirty days with LastModifiedDate.

| improve this answer | |
4

One way is to use a SOQL Date Function: https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

String query = 
    'SELECT Id ' +
    'FROM Case ' + 
    'WHERE OwnerId = \'' + ownerId '\' '
    'AND NOT CreatedDate = LAST_N_DAYS:30 ';

Another way is to calculate the last good date and query records that have a CreatedDate before that date.

DateTime lastDate = System.now().addDays(-30);
String query = 
    'SELECT Id ' +
    'FROM Case ' + 
    'WHERE OwnerId = \'' + ownerId '\' ' +
    'AND CreatedDate < :lastDate'

It seems you are using some tool that might need a DateTime literal in your SOQL Query, and you can use the following which is listed in the link above.

Date, time, and time zone offset    

Format                     |  Example
---------------------------|-----------------------------
YYYY-MM-DDThh:mm:ss+hh:mm  |  1999-01-01T23:01:01+01:00
YYYY-MM-DDThh:mm:ss-hh:mm  |  1999-01-01T23:01:01-08:00
YYYY-MM-DDThh:mm:ssZ       |  1999-01-01T23:01:01Z
| improve this answer | |
  • 2
    The system audit dates are date-times. You'd want to format them as proper date-time values. – sfdcfox Jan 7 '16 at 20:40
  • Updated answer to use DateTime when comparing CreatedDate. Thanks! – Scott Pelak Jan 7 '16 at 23:56

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.