Populate Location Field onLoad - Request-By and onChange - Requested-For
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 03:10 PM
I am trying to call a Script Include, using two different Client Scripts - one onLoad and the other onChange. The end result is to have the Requested-By location populate on load and have the location field change, if the Requested-For field changes.
Here is my code:
SI
var CheckLocation = Class.create();
CheckLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getLocation: function() {
var gr = new GlideRecord("sys_user");
gr.addQuery('sys_id', this.getParameter('sysparm_userID'));
gr.query();
if(gr.next()){
return gr.location.getDisplayValue();
}
},
type: 'CheckLocation'
});
CS (onLoad)
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('CheckLocation');
ga.addParam('sysparm_name', "getLocation");
ga.addParam('sysparm_userID', g_form.getValue('requested_by'));
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('location', answer);
}
});
}
CS (onChange)
Finally, here is the default value for location:
gs.getUser().getRecord().getValue('location.parent');
Nothing seems to be working, although on page load - the location field is mandatory and it shows blank, but the asterisk is grey, as if a value is being returned.
Thoughts?
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 07:22 PM
Hi appstorm,
There's no need to have onLoad script because default value is being set.
onChange()
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CheckLocation');
ga.addParam('sysparm_name', "getLocation");
ga.addParam('sysparm_userID', newValue);
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('location', answer);
}
});
}
Script Include
var CheckLocation = Class.create();
CheckLocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getLocation: function() {
var gr = new GlideRecord("sys_user");
gr.addQuery('sys_id', this.getParameter('sysparm_userID'));
gr.query();
if (gr.next()) {
return gr.location.getDisplayValue();
}
},
type: 'CheckLocation '
});
Execution result:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2022 07:32 PM
Forgot, set the default value on location field to below.
javascript:gs.getUser().getRecord().getValue("location");