how to write schedule job to auto close the catalog task from 14 days from the date of created
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2023 05:15 AM
Hi All,
how to write schedule job to auto close the catalog task from 14 days from the date of creation,
could you suggest how can we achieve this using schedule job
thanks
sid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2023 05:47 AM
Hi Siddharth, can you please check below script and let me know
var gr=new GlideRecord("sc_task");
gr.addEncodedQuery("state=3^closed_atRELATIVEGT@dayofweek@ahead@14");
gr.query();
while(gr.next())
{
gr.state="7";// check your closed state value
gr.update();
}
Regards,
Sandeep Kumar M
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2023 06:31 AM - edited 07-26-2023 11:55 PM
Scheduled job:
var gr = new GlideRecord('sc_task);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2023 07:11 AM
Hi Siddharth use below script
var gr=new GlideRecord("sc_task");
gr.addEncodedQuery("sys_created_onRELATIVEGT@dayofweek@ago@15^sys_created_onRELATIVELT@dayofweek@ago@13");
gr.query();
while(gr.next())
{
gr.state="7";// check your closed state value
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2023 07:30 AM
Hi @siddharth26
Based on what you write, create a Scheduled job that runs daily as seen below
Script follows:
// Scheduled job to delete Catalog tasks
var date2WeeksAgo = new GlideDateTime();
date2WeeksAgo.addDays(-14);
gs.info("closeOldCatalogTasks: Processing records created before " + date2WeeksAgo);
var scTask = new GlideRecord('sc_task');
// don't process records in Closed Complete, Closed Incomplete, or Closed Skipped state
var encodedQueryString = "sys_created_on<="+date2WeeksAgo+"^stateNOT IN3,4,7";
gs.info("closeOldCatalogTasks: Query: " + encodedQueryString);
scTask.addEncodedQuery(encodedQueryString);
scTask.query();
gs.info("closeOldCatalogTasks: Found " + scTask.getRowCount() + " records.");
while (scTask.next()) {
gs.info("closeOldCatalogTasks: Closing Number: " + scTask.number + ", created: " + scTask.sys_created_on);
scTask.state = 3;
// scTask.update();
}
You can test in Scripts Background, and when happy un-comment the 'scTask.update();' line. You can leave or remove the 'gs.info()' lines. If present, you can use 'Script Log Statements' to see what was done when the job ran.