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!

4 REPLIES 4

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'
});

MaxMixali
Mega Sage

Hi PhanitaM I hope that this can help, the list of step and a simple script to complete the activities

 

 

Step 1: Create Timezone Reference Field

  1. Navigate to: System Definition > Dictionary
  2. Click New
  3. Configure:
 
 
Table: [Your table - e.g., incident, sc_req_item, etc.]Type: ReferenceColumn label: Requestor TimezoneColumn name: u_requestor_timezoneReference: Time Zone [sys_time_zone]Max length: 32

Step 2: Create Business Rule to Auto-Populate

  1. Navigate to: System Definition > Business Rules
  2. Click New
  3. Configure:
 
 
javascript
Name: Set Requestor Timezone
Table: [Your table]
Active:Advanced:When: beforeInsert:Update:
Condition: (Leave empty or add: current.u_requestor_timezone.nil())

Script:
(function executeRule(current, previous /*null when async*/) {
    
    // Get the user who opened the record
    if (current.opened_by) {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(current.opened_by)) {
            // Copy user's timezone to the field
            current.u_requestor_timezone = userGR.time_zone;
        }
    }
    
})(current, previous);
  1. Click Submit

Ankur Bawiskar
Tera Patron
Tera Patron

@PhanitaM 

you can use a default value in the field/variable in your form

something like this

javascript: var timezone;
var gr = new GlideRecord('sys_user');
if (gr.get(gs.getUserID())) {
    timezone = gr.getValue('time_zone') || '';
}
timezone;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@PhanitaM 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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