Populating timezone in User table

ramirch
Tera Contributor

Hello!

I have a requirement to populate the timezone field in the Users table from the timezone field in the Location table.

However, the client does not use the Location reference field in the Users table. They created a text field and want to use this. I've created a before BR but does not work.

 

var gr = new GlideRecord('cmn_location');
gr.query();

 while (gr.next()) {

        if (current.getValue('u_location') == gr.getValue('full_name')) {
            current.setvalue('time_zone', gr.time_zone);
        }
    }
 
Please help!
Thank you.
1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru

Hi @ramirch 

Add update(); in your script if that not works, verify with below updated script:

var gr = new GlideRecord('cmn_location');
gr.query();

while (gr.next()) {
if (current.u_location == gr.full_name) {
current.time_zone = gr.time_zone;

current.setWorkflow(false);

current.update();

current.setWorkflow(true);
break; 
}
}

 

View solution in original post

7 REPLIES 7

Nilesh Pol
Tera Guru

Hi @ramirch 

Add update(); in your script if that not works, verify with below updated script:

var gr = new GlideRecord('cmn_location');
gr.query();

while (gr.next()) {
if (current.u_location == gr.full_name) {
current.time_zone = gr.time_zone;

current.setWorkflow(false);

current.update();

current.setWorkflow(true);
break; 
}
}

 

GopikaP
Mega Sage

Hi @ramirch , Please try this: 

var loc = current.getValue('u_location');
var gr = new GlideRecord('cmn_location');
gr.addQuery('full_name', loc);
gr.query();
if(gr.next()) {
    if (gr.time_zone)
        current.setvalue('time_zone', gr.time_zone);
}

Ankur Bawiskar
Tera Patron
Tera Patron

@ramirch 

remember time_zone field has choices in it.

Is user entering the correct choice value in u_location field?

if yes then simply use this

current.time_zone = current.u_location;

AnkurBawiskar_0-1741328986485.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ramirch 

if user is entering location name then query location table and then get the timezone and set it

assuming they enter correct location name then use this

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord("cmn_location");
    gr.addQuery("u_name", current.u_location);
    gr.query();
    if (gr.next()) {
        current.time_zone = gr.time_zone;
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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