Set user time zone to default to location time zone

shill
Mega Sage

I would like to be able to set the time zone for a location and for users that have that location in their user record, to have the location time zone as their default time zone. As our proposed feed of user data does not indicate time zone preferences, just looking for an easier way to default the time zone on the initial and subsequent loads of user data.

For example.
Joe Smith's user data is imported to the user record with a location of Dallas, TX. The system then sets Joe Smith's time zone in his user record to what is indicated as the time zone on the location record for Dallas.

I would think this would be possible with some scripting in the user time zone field default value, or am I way off?

1 ACCEPTED SOLUTION

jacob_kimball
ServiceNow Employee
ServiceNow Employee

Generally, that should work. My suggest would be a business rule run on update of the user record with a condition for when the user location changes. The rule would be something like:



current.time_zone = current.location.time_zone


View solution in original post

10 REPLIES 10

jacob_kimball
ServiceNow Employee
ServiceNow Employee

Generally, that should work. My suggest would be a business rule run on update of the user record with a condition for when the user location changes. The rule would be something like:



current.time_zone = current.location.time_zone


umirza
Kilo Contributor

How could I run this to switch all users to their locations timezone to update all current users?


I setup a scheduled job for the first pass, then a business rule for subsequent changes. The scheduled job is set for one demand so if I see a need to run again I can do it easily.

Here is the code I used to set them initially.

gs.log("Set Users Time Zone> START " + gs.now());
var gr = new GlideRecord('sys_user');
gr.query();
while (gr.next()) {
if (!gr.location.time_zone.nil()) {
gr.time_zone = gr.location.time_zone;
gr.update();
}
}
gs.log("Set Users Time Zone> END" + gs.nowDateTime());
<\code>


Hi there, could you please confirm details of the business rule you utilise for changes - and do you just run this on change of user location?

We had an issue recently where a Location.Time zone value update subsequently meant the update of 787 user record time zone values (due to an insert/update sys_user business rule 'Set Time Zone from Location') and subsequently appears to have caused a 20min instance outage... not good!

This current business rule script is as below and looks to be the same as your scheduled job above (it was created in our instance by a 3rd party consultant when we first implemented SN):

gs.log("Set Users Time Zone> START " + gs.now());
var gr = new GlideRecord('sys_user');
gr.query();
while (gr.next()) {
   if (!gr.location.time_zone.nil()) {
      gr.time_zone = gr.location.time_zone;
      gr.update();
   }
}
gs.log("Set Users Time Zone> END" + gs.nowDateTime());

Thanks for any help in advance.