how to write schedule job to auto close the catalog task from 14 days from the date of created

siddharth26
Tera Guru

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

4 REPLIES 4

sandeep76
Tera Guru

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

anonymous13
Tera Expert


Scheduled job: 

var
gr = new GlideRecord('sc_task);
gr.addEncodedQuery("sys_created_onRELATIVELT@dayofweek@ago@13^sys_created_onRELATIVEGT@dayofweek@ago@14");
gr.query();
while (gr.next()) {
gr.state="7";// check your closed state value

gr.update();
}

saichand1
Tera Contributor

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();
}

Bert_c1
Kilo Patron

Hi @siddharth26 

 

Based on what you write, create a Scheduled job that runs daily as seen below

 

Screenshot 2023-07-26 101921.png

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.