Querying the sys_user table from client script (scope app)

MKelly83
Giga Expert

Hi all

Trying to query "sys_user" table via client script. I want to retrieve the user's id and segment code based on the inputted employee's name (u_formatted_display_name)

I have written the below client script but am getting the below error message

find_real_file.png

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

          return;

    }

var empName = g_form.getDisplayBox('u_emp_name').value;

var gr = new GlideRecord('sys_user');

gr.addQuery('u_formatted_display_name', empName);

gr.query(myCallBack);

function myCallBack(gr) {

if(gr.next()) {

g_form.setValue('u_emp_id', gr.user_name);

g_form.setValue('u_emp_bg', gr.u_segment);

}

}

}

I can dot.walk to these fields on the form but the issue is the business wants to rename fields from whats in the sys_user table and it can't be a global change.

Gaurav Bajaj
Mega Sage

HI Michelle,



Please use get reference callback instead of using Gliderecord in client script.



https://www.servicenowguru.com/scripting/client-scripts-scripting/gform-getreference-callback/




Thanks


Gaurav


Thanks Gaurav



I have tried to use the get reference callback alert is returning "undefined"



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


          return;


    }



var employee = g_form.getReference('u_emp_name', setemployeedetails );



function setemployeedetails(empName) {


alert(employee);


g_form.setValue('u_emp_id', employee.user_name);


g_form.setValue('u_emp_bg', employee.u_segment);


}



}


Lakshmaiah Dump
Giga Expert

Hi,



In client scripts you can't use the GlideRecord Object in client side scripts,



In client side available only g_form and g_user objects to get the form data and current loggedin user info.



Change following code as ur requirement



function onChange(control, oldValue, newValue, isLoading) {
    var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function
}
function doAlert(caller) { //reference is passed into callback as first arguments
  if (caller.vip == 'true')
    alert('Caller is a VIP!');
}



Hit ✅Correct, ��Helpful, or ��Like, Endorsers depending on the impact of the response


@Lakshmaiah Dumpala


Thanks Lakshmaiah



I have tried to use the get reference callback alert is returning "undefined"



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == '') {


          return;


    }



var employee = g_form.getReference('u_emp_name', setemployeedetails );



function setemployeedetails(empName) {


alert(employee);


g_form.setValue('u_emp_id', employee.user_name);


g_form.setValue('u_emp_bg', employee.u_segment);


}



}