- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 05:18 AM
I would like to auto-populate a 'Location' field based on the person selected in the 'New Hire' field. Trying to do this using Default Value, but not working:
javascript:gs.getUser(current.variables.new_hire_name).getRecord().getValue('u_location');
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2018 04:59 AM
I was able to get this working by changing the variable type to 'reference', and using the following code:
function onChange(control, oldValue, newValue, isLoading) {
//Type appropriate comment here, and begin script below
var newHire = g_form.getValue('new_hire_name');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', newHire);
user.query(setLocation);
function setLocation (user){
if ( user.next() ){
g_form.setValue('location_1', user.location);
g_form.setValue('location_2', user.location);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 06:58 AM
Hi
g_form.setValue('location_1', user.location.getDisplayValue());
Mark correct if it helps.
Regards,
Omkar Mone
www.dxsherpa.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 07:26 AM
I tried this, but now it will not populate anything.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 07:29 AM
Hi
Try this ;-
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var caller = g_form.getReference('new_hire_name', setLocation);
}
function setLocation(caller_id) {
if (caller_id)
g_form.setValue('location_1', caller_id.location);
}
Try this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2018 04:59 AM
I was able to get this working by changing the variable type to 'reference', and using the following code:
function onChange(control, oldValue, newValue, isLoading) {
//Type appropriate comment here, and begin script below
var newHire = g_form.getValue('new_hire_name');
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', newHire);
user.query(setLocation);
function setLocation (user){
if ( user.next() ){
g_form.setValue('location_1', user.location);
g_form.setValue('location_2', user.location);
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2018 10:05 AM
It is not best practice to use glideRecord at client side. Use the below code, you can also populate multiple fields using the below codes.
Script Include:
Name : PopulateRequestedfordetails
var PopulateRequestedfordetails = Class.create();
PopulateRequestedfordetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getuserdetails:function(){
var getting=this.getParameter('sysparm_id');
gs.addInfoMessage(getting);
var gr = GlideRecord('sys_user');
gr.get(getting);
var find={};
find.first=gr.location.getDisplayValue() + '';
var json = new JSON();
find = json.encode(find);
return find;
},
type: 'PopulateRequestedfordetails'
});
Client script:
Type: Onchange
UI Type: All
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var userdetails = new GlideAjax('PopulateRequestedfordetails');
userdetails.addParam("sysparm_name", "getuserdetails");
userdetails.addParam("sysparm_id",g_form.getValue('new_hire_name'));
userdetails.getXML(setuserdetails);
function setuserdetails(serverResponse){
var answer= serverResponse.responseXML.documentElement.getAttribute('answer');
//alert(answer);
var obj=JSON.parse(answer);
g_form.setValue('location_1',obj.first);
}
}
Please mark the answer correct or helpful and closed the thread.
Thanks