Automatic deactivation of Standard Change Template through Scheduled Jobs

Paulo Campos Ch
Tera Contributor

What is required is that once the date in the expiration date field is reached, the Active field changes from true to false.

I have this but when I run it, it gives me all of them, including those with a higher date, which should only be those with a date less than the day.

I'm setting it to run daily at 00:01:00

If you can help me see what steps I am omitting or what I am indicating incorrectly.


//##############################
//# VALIDATE EXPIRATION DATE #
//##############################


//GlideDate has methods to work with dates
var chkDate = new GlideDateTime();

//Here what is done is to consult the records that meet the date condition
//expiration
var gr = new GlideRecord('sc_cat_item');
gr.addQuery('active', true);
gr.addQuery('u_expiration_date', '<', chkDate);
gr.query();

//Iterates the records that it finds to send to call the created event and that this
//trigger notification
while(gr.next()){

gr.active = 'false';
gr.update();
}

4 REPLIES 4

Anand Kumar P
Giga Patron

Hi @Paulo Campos Ch,

Use below script it will work 

var chkDate = new GlideDateTime();
chkDate.setDisplayValue(gs.now()); 

var gr = new GlideRecord('sc_cat_item');
gr.addQuery('active', true);
gr.addQuery('u_expiration_date', '<', chkDate);
gr.query();

while (gr.next()) {
gr.active = false;
gr.update();
}

 

Please mark this as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

No, it keeps setting me all on false.
You should only set those dated 10-19-2023 to false because it is already a date shorter or shorter than today

Hi @Paulo Campos Ch ,

Use below script.

var chkDate = new GlideDateTime();
chkDate.setDisplayValue(gs.now());
var gr = new GlideRecord('sc_cat_item');
gr.addQuery('active', true);
gr.addQuery('u_expiration_date', '<', chkDate);
gr.addQuery('u_expiration_date', '>=', '2023-10-10 00:00:00'); // Greater than or equal to October 10, 2023
gr.addQuery('u_expiration_date', '<=', '2023-10-19 23:59:59'); // Less than or equal to October 19, 2023
gr.query();
while (gr.next()) {
    gr.active = false;
    gr.update();
}

Please mark this as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

 

But it is limiting it to those dates, and I would have to log in every month to change the work schedule, and that is not the idea.

What I want is for the script to validate the expiration date with the current day (today), since as indicated by the scheduled job it will be performing this validation daily at a time of 00:01:00

 

gr.addQuery('u_expiration_date', '>=', '2023-10-10 00:00:00'); // Greater than or equal to October 10, 2023
gr.addQuery('u_expiration_date', '<=', '2023-10-19 23:59:59'); // Less than or equal to October 19, 2023