I am trying auto populate user location on load of a record producer . using this script but it is not populating
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 02:43 AM
I am using this script for the same :
function onLoad() {
var id = g_form.getValue('caller_id');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', id);
user.query();
if (user.next()) {
g_form.setValue('location', user.location);
}
}
Also trying to change the location is caller is changed in the same record producer and using this script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == '' || newValue == null) {
g_form.setValue('location', '');
return;
}
if (!g_form.hasField('location'))
return;
var caller = g_form.getReference('caller_id', setLocation);
}
function setLocation(caller) {
if (caller)
g_form.setValue('location', caller.location);
}
both are not working. could anyone suggest why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 02:52 AM
Hi,
you can use default value in the variable
javascript: gs.getUser().getLocation();
Update onChange as this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('location');
return;
}
if(oldValue != newValue)
var caller = g_form.getReference('caller_id', setLocation);
}
function setLocation(caller) {
if (caller)
g_form.setValue('location', caller.location);
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 03:05 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 03:41 AM
Rahul,
Just want to make sure. Is "Caller" a reference to sys_user table and Location a reference to cmn_location table?
Following is my client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('location');
return;
}
if (oldValue != newValue) {
g_form.getReference('caller_id', setLocation);
}
function setLocation(caller) {
if (caller)
g_form.setValue('location', caller.location);
}
}
Execution result. Location is being filled in.
Just to make sure, if viewing in Service Portal, is "UI Type" set to "All"?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2022 04:39 AM
Hi,
it should work provided the logged in user has location
I believe you want logged in user's location to be populated
Then if caller is changed the location can be fetched using onChange client script
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader