Populate user location using default value

nicolemccray
Tera Expert

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');

 

1 ACCEPTED SOLUTION

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);
        }
    }
}

View solution in original post

15 REPLIES 15

In the Default Value for the location field:

 

find_real_file.png

Hi 

so gs.getUser() gets the current logged in user. In this case you will have if you try impersonating you will see the difference.

I recommend you to write a script include and Client script  to get the current location of the user on the new_hire_name feild.

 

 

you can write a script include for this and return the value, If you want I can help you with writing one.

Manan Raval
Giga Expert

Hi Nicole,

You are getting your location is because gs.getUser() does not consider any parameters. It will take the current logged in user as its glidereocrd object. To achieve your requirement, I would suggest you to write a script include and pass the new_hire_name to the script include method and get the location of the user and return it.

 

Regards,

Manan Raval

nicolemccray
Tera Expert

I was able to get this working with a client script, however, it is displaying as a sys_id.  How can I get the actual location to display?:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//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();
if ( user.next() )
{
g_form.setValue('location_1', user.location);
}