Anand Dhage
Anand Dhage

Reputation: 119

How to get latest updated record from sys_user table which is modified at or after certain time stamp in ServiceNow

I want to fetch record from the sys_user table which is updated at or after certain time stamp. for that I have created rest request as

https:/service-now.com/api/now/v1//table/sys_user?sysparm_query=sys_updated_on>=javascript:gs.dateGenerate('2017-10-30','01:25:00')

I had converted current time which is in IST format into GMT and pass it to dateGenerate() function.

Problem statement - I don't want to convert the IST to GMT, is there any way by which i can identify ServiceNow instance time zone at runtime and convert given time into that time stamp and get the users. If i can pass this date and time in UTC format.

Upvotes: 1

Views: 1877

Answers (1)

Tim Woodruff
Tim Woodruff

Reputation: 601

Ahoy!

This is a great question, and something that's quite difficult in ServiceNow (dealing with time-zones). As such, I've written a tool to manage this for you. It's totally free!

The tool is called TimeZoneUtil, and can be found here: https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil

You simply need to initialize a GlideDateTime object, set its' time-zone to IST, use setDisplayValue() to set its' time based on IST current time, then use .getValue() to get that same time in system time. This is because getDisplayValue()/setDisplayValue() work based on time-zone, whereas setValue()/getValue() always return the corresponding system time.


EDIT: In order to make this a little more clear, I'll provide some example usage below.

var tz = new TimeZoneUtils(); //initialize with current time
gs.print(tz.getOffsetHours()); //prints out "-7" in my instance, as the system time is in Pacific.
tz.setTimeZone('Asia/Kolkata'); //sets the time-zone to IST/UTC+5.5
gs.print(tz.getOffsetHours()); //prints "5.5".
gs.print(tz.getGDT().getDisplayValue()); //Prints the current time in IST (2017-11-01 20:52:31 at the moment).
gs.print(tz.getGDT().getValue()); //Prints the current time in system time (2017-11-01 15:23:12 at present).

gs.print(new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()); //Single line, also prints current time in IST: 2017-11-01 20:52:31

The first 6 lines there, demonstrate basic usage and explain how it works. The eighth line however, demonstrates usage on a single line, which you can use inside a query string. For example:

sysparm_query=sys_updated_on>=javascript:new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()

Hope this helps!


Tim Woodruff

Author, Learning ServiceNow & Building Powerful Workflows

Owner/Founder, SN Pro Tips

Upvotes: 0

Related Questions