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

SANDEEP28
Mega Sage

@Priyansh_98Use below script. Also remove setLimit(1) when you are putting this script into scheduled job script as you will have multiple incidents.

var grIncident = new GlideRecord('incident');
grIncident.addEncodedQuery('state=1');
grIncident.setLimit(1);
grIncident.query();
while (grIncident.next())
{
   
    var ticket_updated_time= new GlideDateTime(grIncident.sys_updated_on);
    var today = new GlideDateTime();
    var duration_seconds = gs.dateDiff(ticket_updated_time, today, true);
    var duration_days = Math.round(duration_seconds/ (86400));

    gs.info(duration_days); 
}

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Hi @SANDEEP28 ,

thanks for your response, it is working fine for an incident table.. but when i am using a custom table for the same requirement. the duration time is different. below are the details...

script : 

 

var grIncident = new GlideRecord('x_aai_gbs_finanapp_gbs_finance');
grIncident.addEncodedQuery('state=19');
grIncident.setLimit(5);
grIncident.query();
while (grIncident.next())
{
   
    var ticket_updated_time= new GlideDateTime(grIncident.sys_updated_on);
    var today = new GlideDateTime();
    var duration_seconds = gs.dateDiff(ticket_updated_time, today, true);
    var duration_days = Math.round(duration_seconds/ (86400));
gs.print('number : '+grIncident.number);

gs.print('today : '+today);
gs.print('ticket_updated_time : ' +ticket_updated_time);
gs.print('duration_seconds : ' +duration_seconds);
gs.print('duration_days : ' +duration_days);



    //gs.info(duration_days);
}
 
and the Output is : 
*** Script: number : GBS0035688
*** Script: today : 2023-07-21 12:13:45
*** Script: ticket_updated_time : 2023-04-21 03:19:24
*** Script: duration_seconds : 7894461
*** Script: duration_days : 91
*** Script: number : GBS0039893
*** Script: today : 2023-07-21 12:13:45
*** Script: ticket_updated_time : 2023-04-20 18:32:05
*** Script: duration_seconds : 7926100
*** Script: duration_days : 92
*** Script: number : GBS0039685
*** Script: today : 2023-07-21 12:13:45
*** Script: ticket_updated_time : 2023-04-21 03:29:31
*** Script: duration_seconds : 7893854
*** Script: duration_days : 91
*** Script: number : GBS0035649
*** Script: today : 2023-07-21 12:13:45
*** Script: ticket_updated_time : 2023-04-21 03:12:31
*** Script: duration_seconds : 7894874
*** Script: duration_days : 91
*** Script: number : GBS0040257
*** Script: today : 2023-07-21 12:13:45
*** Script: ticket_updated_time : 2023-04-21 16:31:31
*** Script: duration_seconds : 7846934
*** Script: duration_days : 91

 

@Priyansh_98 What is issue here ? could you please elaborate.

Math.round will round your output. Like if output is 2.95 then it will round up to 3

Hi @SANDEEP28 ,

please check the output, in that you will see the difference between today's date and ticket_updated_time, even if the difference is just a 1 day still it is showing duration days = 91.

that i want to fix.