Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

Hi 

g_form.setValue('location_1', user.location.getDisplayValue());

 

Mark correct if it helps.

 

Regards,

Omkar Mone

www.dxsherpa.com

I tried this, but now it will not populate anything.

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.

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

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