Requestor TimeZone field on Change form
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
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!
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
@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'
});
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
45m ago
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
- Navigate to: System Definition > Dictionary
- Click New
- 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
- Navigate to: System Definition > Business Rules
- Click New
- 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);
- Click Submit
