- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:23 AM
Hi,
I created PUT rest message and I need to pass some parameters to the json. one of the parameters is the field sys_created_on of tyep Date/Time on the incident table.
When Im trying to test the REST Message Im getting error saying:
"Expected unix time" regarding the value of sys_created_on that im passing.
How can I convert this field's value to UNIX time?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:46 AM
In ServiceNow, you can convert a Date/Time field to UNIX time (also known as Epoch time) using the GlideDateTime class.
Here's an example of how you can convert the sys_created_on field to UNIX time:
// Assume incidentGR is a GlideRecord on the incident table
var sysCreatedOn = incidentGR.getValue('sys_created_on');
// Create a GlideDateTime object
var gdt = new GlideDateTime(sysCreatedOn);
// Get the UNIX timestamp
var unixTime = gdt.getNumericValue();
// Now, you can pass unixTime to your REST message
In this script, getNumericValue() method returns the number of milliseconds since January 1, 1970 00:00:00 GMT. This is the UNIX timestamp, but in milliseconds.
If the API expects the UNIX timestamp in seconds, simply divide the result by 1000:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:35 AM
var gr = new GlideRecord('incident');
gr.get('sys_id', 'YOUR_INCIDENT_SYS_ID');
var createdOn = gr.getValue('sys_created_on');
var glideDateTime = new GlideDateTime();
glideDateTime.setDisplayValue(createdOn);
var unixTimestamp = glideDateTime.getNumericValue();
Try by using this script, hope you will get your answer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:39 AM
var gr = new GlideRecord('incident');
gr.get('sys_id', 'YOUR_INCIDENT_SYS_ID');
var createdOn = gr.getValue('sys_created_on');
var glideDateTime = new GlideDateTime();
glideDateTime.setDisplayValue(createdOn);
var unixTimestamp = glideDateTime.getNumericValue();
Try by using this script, Hope you will get your answer for the posted question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:46 AM
In ServiceNow, you can convert a Date/Time field to UNIX time (also known as Epoch time) using the GlideDateTime class.
Here's an example of how you can convert the sys_created_on field to UNIX time:
// Assume incidentGR is a GlideRecord on the incident table
var sysCreatedOn = incidentGR.getValue('sys_created_on');
// Create a GlideDateTime object
var gdt = new GlideDateTime(sysCreatedOn);
// Get the UNIX timestamp
var unixTime = gdt.getNumericValue();
// Now, you can pass unixTime to your REST message
In this script, getNumericValue() method returns the number of milliseconds since January 1, 1970 00:00:00 GMT. This is the UNIX timestamp, but in milliseconds.
If the API expects the UNIX timestamp in seconds, simply divide the result by 1000:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-04-2024 12:46 AM
Hi @Alon Grod,
You can convert the sys_created_on to UNIX with this script and then you can pass this as a parameter.
// Get the value of the sys_created_on field
var createdOn = incident.getValue('sys_created_on');
// Convert to GlideDateTime object
var glideDateTime = new GlideDateTime();
glideDateTime.setDisplayValue(createdOn);
// Get UNIX time (milliseconds since the Unix epoch)
var unixTime = glideDateTime.getNumericValue();
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!