schedule job

lalithkumar
Tera Contributor

Create a Scheduled job to close all the resolved incidents of last month which runs every
month on 3rd at 6am.can anyone explain be briefly about it 

1 ACCEPTED SOLUTION

Shaqeel
Mega Sage

Hi @lalithkumar 

 

I think you are new and need step-by-step instructions.

 

Here you go.

 Navigate to schedule jobs system definition>>create new.

Enter the details as follows:

Shaqeel_0-1733387224358.png

 

Here is the script:

// Get the first and last days of the previous month
var now = new GlideDateTime();
now.addMonthsUTC(-1); // Move to the previous month
now.setDayOfMonthUTC(1); // Set to the first day
var startOfMonth = now.getValue();
 
now.addMonthsUTC(1);
now.setDayOfMonthUTC(1); // Set to the first day of the current month
now.addSecondsUTC(-1); // Set to the last day of the previous month
var endOfMonth = now.getValue();
 
// Query incidents resolved in the previous month
var gr = new GlideRecord('incident');
gr.addQuery('resolved_at', '>=', startOfMonth);
gr.addQuery('resolved_at', '<=', endOfMonth);
gr.addQuery('state', '6'); // Resolved state
gr.query();
 
// Close each incident
while (gr.next()) {
    gr.state = '7'; // Closed state
    gr.close_code = 'Closed/Resolved by automation';
    gr.close_notes = 'Automatically closed by Scheduled Job on 3rd of the month.';
    gr.update();
}
 
gs.print('Scheduled Job: Closed all resolved incidents of last month.');

 

***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************

 

 

Regards

Shaqeel

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel

View solution in original post

1 REPLY 1

Shaqeel
Mega Sage

Hi @lalithkumar 

 

I think you are new and need step-by-step instructions.

 

Here you go.

 Navigate to schedule jobs system definition>>create new.

Enter the details as follows:

Shaqeel_0-1733387224358.png

 

Here is the script:

// Get the first and last days of the previous month
var now = new GlideDateTime();
now.addMonthsUTC(-1); // Move to the previous month
now.setDayOfMonthUTC(1); // Set to the first day
var startOfMonth = now.getValue();
 
now.addMonthsUTC(1);
now.setDayOfMonthUTC(1); // Set to the first day of the current month
now.addSecondsUTC(-1); // Set to the last day of the previous month
var endOfMonth = now.getValue();
 
// Query incidents resolved in the previous month
var gr = new GlideRecord('incident');
gr.addQuery('resolved_at', '>=', startOfMonth);
gr.addQuery('resolved_at', '<=', endOfMonth);
gr.addQuery('state', '6'); // Resolved state
gr.query();
 
// Close each incident
while (gr.next()) {
    gr.state = '7'; // Closed state
    gr.close_code = 'Closed/Resolved by automation';
    gr.close_notes = 'Automatically closed by Scheduled Job on 3rd of the month.';
    gr.update();
}
 
gs.print('Scheduled Job: Closed all resolved incidents of last month.');

 

***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************

 

 

Regards

Shaqeel

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel