Glide record using gs.daysAgo and UTC time

catho
Tera Expert

I need to trigger a reminder email on 7 days to users who have had a survey created, but not answered it.

I have:

1) created an event for this

2) created an email notification (with the unique survey link for the user) triggered by the event

3) created a scheduled job - to identify the records and fire the event.

 

I have created a heap of surveys in our DEV instance, and updated sys_created_on  to have records on a range of days so I can validate the script.

 

I have been using a background script to identify the records that will trigger notification - and they seem to be out by 10 hours (we are in UTC).

 

Script:

var gr = new GlideRecord('asmt_assessment_instance');gr.query();


while (gr.next()) {

 

if ((gr.state == 'ready') && (gr.sys_created_on <= gs.daysAgo(6)) && (gr.sys_created_on >= gs.daysAgo(7))) {

 

gs.print (gr.number + " " + gr.sys_created_on);

}

 

}

 

Records found:

*** Script: SURVEY0010835 2019-06-14 04:23:43 
*** Script: SURVEY0010821 2019-06-14 20:23:43
*** Script: SURVEY0010822 2019-06-14 10:23:43
*** Script: SURVEY0010826 2019-06-14 22:23:43
*** Script: SURVEY0010817 2019-06-14 22:25:16

My issue is that this is not the actual dates that they were created >> see below
Background Script                    - SN when viewing survey
SURVEY0010835 2019-06-14 04:23:43 - 14-06-2019 14:23:43
SURVEY0010821 2019-06-14 20:23:43 - 15-06-2019 06:23:43
SURVEY0010822 2019-06-14 10:23:43 - 14-06-2019 20:23:43
SURVEY0010826 2019-06-14 22:23:43 - 15-06-2019 08:23:43
SURVEY0010817 2019-06-14 22:25:16 - 15-06-2019 08:25:16

Is there anything I'm missing to easily identify items from 7 days ago? I have seen scripts a lot more complicating using date & time functions ..... I was hoping to do it in a simple way if possible as I'm not super strong at scripting.

 

 

1 ACCEPTED SOLUTION

Example using Incident - email Assigned to user until resolution every 7 days


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

Mike Patel
Tera Sage

change below and see what you get

gs.print (gr.number + " " + gr.sys_created_on.getDisplayValue());

The SN Nerd
Giga Sage
Giga Sage

gs.daysAgo() returns a GlideDateTime object.
sys_created_on returns a string.
You are comparing apples and oranges.

All values are stored in the database as UTC.
They are displayed in the Users local time zone of system default.

 

I'm not going to provide you with the correct API here, because I don't think this is the best solution for this problem.

Use Flow Designer. 

Trigger on insert of asmt_assessment_instance.

Enter loop.

Wait 7 days.

If asmt_assessment_instance is still not completed, continue in loop.

 


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

catho
Tera Expert

Thanks Paul - that's a good idea.

 

I haven't used flow designer yet - I think now would definitely be the right time to have a go!

 

cheers,

Cath

Example using Incident - email Assigned to user until resolution every 7 days


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022