Client Script to set location of user
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 03:00 PM
Hello,
Issue: I have a catalog item with a location field which I want to be set on load based on requested_for location. More specifically I want the parent location displayed.
Client script is:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || (g_form.isLiveUpdating && g_form.isLiveUpdating()))
return;
if (newValue == '') {
g_form.setValue('requested_region', '');
return;
}
var caller = g_form.getReference('requested_by', setLocation);
}
function setLocation(caller) {
if (caller)
g_form.setValue('requested_region', caller.location);
//g_form.setValue('phone_number', caller.phone);
}
Currently it loads the city the requested_for but I need the Parent location
I would think its g_form.setValue('requested_region', caller.location.parent); but its not.
Any help?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 03:54 PM
For onLoad do
Add default value to variable - gs.getUser().getRecord().getValue('location.parent');
for onChange
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_user', g_form.getValue('requested_by'));
ga.getXMLAnswer(function(answer){
// Save the return to a global variable
var info = JSON.parse(answer);
g_form.setValue('requested_region', info.location);
//g_form.setValue('phone_number', info.phone);
});
}
Script Includes
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord("sys_user");
gr.get(user);
var location = gr.location.parent;
var phone = gr.phone;
var response = {};
response.location = location;
response.phone = phone;
return JSON.stringify(response);
},
type: 'u_userInfoAjax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2019 07:15 PM
Hi,
you can do only 1 level of dot walking using getReference() callback; to go more into deep level of dot walk you would require glideajax as Mike suggested
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader