Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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); 
}
share|improve this question
    
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 at 21:31
up vote 6 down vote accepted
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.

share|improve this answer

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
share|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 at 20:40
    
Updated answer to use DateTime when comparing CreatedDate. Thanks! – ʞɐʃǝԀ ʇʇoɔS Jan 7 at 23:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.