Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Requestor TimeZone field on Change form

PhanitaM
Tera Contributor

Hi Team , 

I have a requirement where user want 'Requestor TimeZone'  field on the form which will auto-populate opened  by user's timezone.  Can someone please help me how to achieve this functionality.

 

Thank you!

1 REPLY 1

Sandeep Rajput
Tera Patron
Tera Patron

@PhanitaM You can use a combination of a client script and script include to populate the opened for user's timezone.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (!newValue) {
        g_form.clearValue('u_requestor_timezone');
        return;
    }

    // GlideAjax to fetch the user's timezone from sys_user
    var ga = new GlideAjax('UserDetailsUtils');  
    ga.addParam('sysparm_name', 'getUserTimeZone');
    ga.addParam('sysparm_user', newValue);
    
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            g_form.setValue('u_requestor_timezone', answer);
        } else {
            g_form.clearValue('u_requestor_timezone');
        }
    });
}

 

Script Include:

var UserDetailsUtils = Class.create();
UserDetailsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    /**
     * GlideAjax callable method to return a user's time_zone value.
     * Expects sysparm_user = sys_id of the user. If omitted, returns current user's timezone.
     */
    getUserTimeZone: function() {
        var userSysId = this.getParameter('sysparm_user');
        if (!userSysId)
            userSysId = this.getUserID();

        var gr = new GlideRecord('sys_user');
        if (gr.get(userSysId)) {
            // use getValue to avoid returning GlideElement
            return gr.getValue('time_zone') || '';
        }
        return '';
    },

    type: 'UserDetailsUtils'
});