How to set Timezone to be related to the User location?

Community Alums
Not applicable

Hi Connections,

 

I have a requirement to set the time zone is set up based on the user's location. How can I achieve this and how can I test it...?

 

Appreciate any help.

 

Best Regards,

Rafmine.

8 REPLIES 8

If you have the location for each user stored in the location field, you could use a geocode API, to determine the Time Zone of that location, and then set it for the user.

 

Here is an example I created using https://opencagedata.com/

Note that you will need to sign up there, and retrieve an API Key to use in below code:

 

var users = new GlideRecord('sys_user');
users.addActiveQuery();
users.addNotNullQuery('location');
users.setLimit(5)
users.query();

while (users.next()){

// Retrieve the user's location
var location = getUserLocation(users);

// Determine the timezone based on the location
  var timezone = determineTimezone(location)

// Set the user's timezone to the determined timezone
setUserTimezone(timezone, users);

}


function getUserLocation(user) {
  return user.location;
}

function determineTimezone(location) {
  var api_key = '<<YOUR API KEY HERE>>';

  var query = location.city.getDisplayValue() + ',+' + location.country.getDisplayValue();

  var api_url = 'https://api.opencagedata.com/geocode/v1/json'

  var request_url = api_url
    + '?'
    + 'key=' + api_key
    + '&q=' + encodeURIComponent(query)
    + '&pretty=1'

  // see full list of required and optional parameters:
  // https://opencagedata.com/api#forward

  var request = new sn_ws.RESTMessageV2();
  request.setHttpMethod('get');
  request.setEndpoint(request_url);
 
 var response = request.execute();
        var httpResponseStatus = response.getStatusCode();
      // see full list of possible response codes:
    // https://opencagedata.com/api#codes

    if (httpResponseStatus === 200){
      // Success!
      var data = JSON.parse(response.getBody());
      gs.info( data.results[0].annotations.timezone.name + ' for ' + query);
      return data.results[0].annotations.timezone.name;

    } else if (httpResponseStatus <= 500){
      // We reached our target server, but it returned an error

      gs.error("unable to geocode! Response code: " + httpResponseStatus);
      var data = JSON.parse(response.getBody());
      gs.error('error msg: ' + data.status.message);
    } else {
      gs.error("server error");
    }

}

function setUserTimezone(timezone, user) {
   user.time_zone = timezone;
   user.update();
}

 

In this example I used it as a background script, to fill the location of all (limited to 5) users, but you could create this as Business Rule as well.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

Can you explain the business requirement for this?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi Ankur,

 

I have provided the info below, kindly refer.

 

Best Regards,

Rafmine.

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Community Alums 

 

Seems your statement is incomplete , please provide more information. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************