notification after 3 days in servicenow

Priyansh_98
Tera Guru

I have to send a notification to the user when the ticket is 3 days old and the state is in awaiting info state and when a ticket is in the "awaiting info" state for more than 6 days, then the ticket should be canceled.. i have created scheduled jobs and added this script...

the problem is I am not able to get the days count even though the ticket is updated recently also it does not show the correct days count.

i am using an updated date field for taking the difference between today's date and updated date for trigeering the condition.

script : 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('state=3');
gr.setLimit(1);
gr.query();
while (gr.next())
{
    var updated = gr.sys_updated_on;
    var ticket_updated= new GlideDateTime(gr.sys_updated_on);
    var today = new GlideDate();
    var duration_seconds = gs.dateDiff(updated, today, true);
//  var duration_days = Math.round(dur_seconds / (3600 * 24));

    gs.print(''+gr.number);
    gs.print('updated '+updated);
 
    gs.print(updated);
    gs.print('today'+today);
    gs.print('secods' +duration_seconds );
    //gs.print('dur_days' +duration_days );

}
can someone please help me with this requirement?
12 REPLIES 12

Hello @Priyansh_98 

I did not test it, but you can configure something like this,

praneeth7_1-1690283272347.png

I have used send email action here, if you want send some notification also you can replace it with your notification.

If you find this useful Please mark my answer Helpful & Accepted Solution

Thank you

Harish KM
Kilo Patron
Kilo Patron

Hi the below is the schedule job which will close after 10 days when there is no repsonse, modify this as per your requirement

checkINC();
function checkINC() {
// This INC job will check all non updated incidents for last 10 days and closes the incident. A OOB closed notification will be sent by comments
//Incase Incident get any comments/worknotes. The timer will reset.
var updatedDate = '';
var today = new GlideDateTime();
var today_date = today.getDate();
var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addQuery('state', '4');
gr.query();
while (gr.next()) {
updatedDate = new GlideDateTime(gr.getValue('sys_updated_on'));
updatedDate = updatedDate.getDate();
var dur = GlideDateTime.subtract(updatedDate, today_date).getRoundedDayPart();
if (dur == '10') {
gr.close_code = 'No Response'; // Unknown
gr.close_notes = "Incident Closed due to no response from Caller.";
gr.u_cause = "Knowledge/Training Related";
gr.state = 7; //closed
gr.active = false;
gr.comments = 'Incident automatically closed after ' + 10 + ' days in a Awaiting User Information status';
// gs.eventQueue("inc.reminder", inc, inc.caller_id, gs.getUserName());
gr.update();
}
}
}

Regards
Harish

Riya Verma
Kilo Sage
Kilo Sage

Hi @Priyansh_98 ,

Hope you are doing great.

 

Please try using below script :

 

var gr = new GlideRecord('incident');
gr.addQuery('state', 'awaiting info');
gr.addQuery('sys_updated_on', '<', gs.daysAgo(3));
gr.query();

while (gr.next()) {
    var updated = gr.sys_updated_on;
    var ticket_updated = new GlideDateTime(gr.sys_updated_on);
    var today = new GlideDateTime();
    var duration_seconds = gs.dateDiff(ticket_updated, today, true);

    gs.print('Ticket Number: ' + gr.number);
    gs.print('Updated Date: ' + updated);
    gs.print('Today: ' + today);
    gs.print('Seconds since last update: ' + duration_seconds);

    if (duration_seconds > (6 * 24 * 60 * 60)) {
        // Cancel the ticket as it has been in "awaiting info" state for more than 6 days
        gr.state = 4; // Assuming 4 is the code for "canceled" state
        gr.update();
        gs.print('Ticket has been canceled.');
    } else if (duration_seconds > (3 * 24 * 60 * 60)) {
        // Send notification as the ticket is 3 days old and still in "awaiting info" state
        gs.print('Sending notification to user.');
        // Add your notification logic here.
    }
}

 

 
 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma