ServiceNow Not Setting Correct Time on a Record Created by Scheduled Job
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 04:58 AM
What does ServiceNow have such a hard time with date/times?
If I run the following code in a background script, it correctly returns the date/time that is three days in the future at 5:00 AM (as shown below):
var dt = new GlideDate();
dt.addDays(3);
//set time to be 5:00 AM
var dt2 = new GlideDateTime();
dt2.setValue(dt.getValue() + ' 05:00:00');
gs.print(dt2);
Results:
However, if I place that EXACT same code in a Scheduled Job to request a clone, like this:
//Creates weekly clone of DEV2 environment.
//Runs in Friday to create Clone for following Monday morning
//go exactly 3 days in the future from current date
var dt = new GlideDate();
dt.addDays(3);
//set time to be 5:00 AM
var dt2 = new GlideDateTime();
dt2.setValue(dt.getValue() + ' 05:00:00');
var gr=new GlideRecord("clone_instance");
gr.initialize();
gr.target_instance="67b20ec61b602d10a4ed993f034bcb44"; //DEV2 instance
gr.profile="ee751137db74e510c5201c791396192e"; //System Profile
gr.scheduled=dt2; //so that it runs on Monday morning
gr.email="xyz@xyz.com";
gr.insert();
It sets it to 12:00 AM instead of 5:00 AM, as shown here in the Clone that was created by this Scheduled Job today:
I noticed that on the Scheduled Job, it gives you this message:
I tried two times, once using my specific time zone ("US/Eastern") and one using the "Use System Time Zone" setting, and it made no difference. Both returned a time of 12:00 AM on the clone.
Why does ServiceNow have such a hard time with times?
Does anyone know why this would happen and how to fix it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 05:06 AM
Hi @jmiskey ,
ServiceNow uses UTC time for all date/time fields, which means that time zone conversion may be required when working with date/time fields. This can often lead to unexpected results if time zone conversion is not properly handled.
In the code provided, the GlideDate and GlideDateTime objects are created using the system time zone, which may not be the same as the time zone used by the Scheduled Job. This can cause issues with the time value being set incorrectly, as seen in the cloned instance.
One way to ensure that the correct time zone is used is to explicitly set the time zone for the GlideDate and GlideDateTime objects. For example, the following code sets the time zone to "US/Eastern":
var dt = new GlideDate();
dt.addDays(3);
dt.setTZ('US/Eastern');
var dt2 = new GlideDateTime();
dt2.setValue(dt.getValue() + ' 05:00:00');
dt2.setTZ('US/Eastern');
Additionally, it is important to note that the time zone used by the Scheduled Job can be configured in the Scheduled Job record itself. If the time zone is set to a specific value, it will override the system time zone and use the specified time zone instead. It may be worth checking the Scheduled Job record to ensure that the correct time zone is being used.
Overall, the issue with date/time values in ServiceNow is often related to time zone conversion and ensuring that the correct time zone is used throughout the system. By explicitly setting the time zone for date/time objects and ensuring that the correct time zone is used in all relevant areas of the system, these issues can be avoided.
Regards,
Shravan
Shravan
Please mark this as helpful and correct answer, if this helps you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 05:23 AM
Unfortunately, that did not work. So I tried your code in a Background Script, and it gave me an error, saying "
Javascript compiler exception: Can't find method com.glide.glideobject.GlideDateTime.setTZ(string).
So it does not appear to like the use of "setTZ". It does not seem to be recognizing it.
You also said:
"Additionally, it is important to note that the time zone used by the Scheduled Job can be configured in the Scheduled Job record itself. If the time zone is set to a specific value, it will override the system time zone and use the specified time zone instead. It may be worth checking the Scheduled Job record to ensure that the correct time zone is being used."
Note that I mentioned I already tried setting the Time Zone in the Scheduled Job record itself, trying two different options, and it made absolutely no difference:
(quote from my original post):
"I tried two times, once using my specific time zone ("US/Eastern") and one using the "Use System Time Zone" setting, and it made no difference. Both returned a time of 12:00 AM on the clone."
Maybe I wasn't totally clear that I was talking about the "Time zone" record on the Scheduled Job record.
So, it still appears I am still stuck, in search of working solution for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 07:42 AM
So, I have a workaround that works. If I just use this:
var dt = new GlideDateTime();
dt.addDays(3);
and set this Scheduled Job to run at exactly 5:00 AM, then it will simply add 3 days to the exact day/time that it runs, so I can get it to create the record for 5:00 AM in three days.
So I will just use that for now. Like I said, it works, though I would still like to know how to get someone to run at a specific time in the future without using this "hack".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2023 06:57 AM
Hi @jmiskey ,
May be your script creating clone request with 5 am. But that time value is 5 am in GMT timezone. If your profile system timezone is not same as UTC, then ServiceNow will covert time value from GMT to your timezone. So you will different time on clone request form.
Change your system timezone into GMT and open clone request form. You will see time as 5 AM.
If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank you