GlideRecord returns sys_id instead of value (getDisplayValue)

Matki
Tera Contributor

Hi everyone,

I have a client script which maps the information entered by the user with the information stored in the user record. 

 

For the reference field "department" it only shows me the sys_id and not the name.

Matki_1-1671611397393.png

So I have tried gr.getValue()/gr.getDisplayValue()/gr.department.name but it is not working. It returns "undefined" or nothing.

 

My code:

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', newValue);
gr.query(myCallbackFunction); //Execute the query with callback function

//After the server returns the query recordset, continue here
function myCallbackFunction(gr) {

if (gr.next()) { //While the recordset contains records, iterate through them
g_form.setValue('user_id', gr.u_extensionattribute13);
g_form.setValue('department_of_user', gr.getDisplayValue('department');

}
}
//Type appropriate comment here, and begin script below

}

 

Thanks in advance for help!

Greetings 

1 ACCEPTED SOLUTION

Deepika18
Tera Guru

Hi Matki,

 

You can achieve this by calling script include in client script using GlideAjax.

Please find the below script.

 

OnChange Client Script on Requested For field :

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


var dept = new GlideAjax("ScriptInclude Name");
dept.addParam("sysparm_name", "Function Name");
dept.addParam("sysparm_dept", newValue); //sysparm_dept is the parameter declared in Script Include.
dept.getXML(DO); // DO is the Callback function

function DO(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue("department", answer);
}
}

 

Client Callable Script Include :

var AutoDept = Class.create();
AutoDept.prototype = Object.extendsObject(AbstractAjaxProcessor, {


Department : function(){
var dep = this.getParameter("sysparm_dept"); //sysparm_dept is the parameter used in client script
var dept = new GlideRecord("sys_user");
if(dept.get(dep)){
var result = dept.getDisplayValue("department");
}
return result;
},


type: 'AutoDept'
});

 

This will auto-populate the Department name based the user selected in Requested For.

 

Please feel free to ask incase of any queries from the above script.

Please mark it as correct if it resolved your query.

 

Regards,

Deepika.

View solution in original post

3 REPLIES 3

Veer
Tera Guru

Hello @Matki  using GlideRecord on client script is not a best practice, try using GlideAjax, however to you query try using below the line.

 

 

 

 

gr.department.getDisplayValue();

 

 

 

if the issue still exists then check in the department table, which field is made as display in the dictionary. also, check how data is displayed in the user table.


By default in any table fields with column_name as 'name' or 'u_name' are made as a display field. but if this has been changed to sys_id then it will pull data in sys_id in reference fields. 

checking the dictionary configuration helps to resolve the issue.

 

dhruvd
Tera Expert

Hi @Matki ,

 

Could you try 

"g_form.setValue('department_of_user', gr.department.getDisplayValue('department');"

 

An observation:

Instead of using glide record in client script, could you try GlideAjax as it'll be much more efficient.

 

Thanks,
Dhruv

Deepika18
Tera Guru

Hi Matki,

 

You can achieve this by calling script include in client script using GlideAjax.

Please find the below script.

 

OnChange Client Script on Requested For field :

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


var dept = new GlideAjax("ScriptInclude Name");
dept.addParam("sysparm_name", "Function Name");
dept.addParam("sysparm_dept", newValue); //sysparm_dept is the parameter declared in Script Include.
dept.getXML(DO); // DO is the Callback function

function DO(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue("department", answer);
}
}

 

Client Callable Script Include :

var AutoDept = Class.create();
AutoDept.prototype = Object.extendsObject(AbstractAjaxProcessor, {


Department : function(){
var dep = this.getParameter("sysparm_dept"); //sysparm_dept is the parameter used in client script
var dept = new GlideRecord("sys_user");
if(dept.get(dep)){
var result = dept.getDisplayValue("department");
}
return result;
},


type: 'AutoDept'
});

 

This will auto-populate the Department name based the user selected in Requested For.

 

Please feel free to ask incase of any queries from the above script.

Please mark it as correct if it resolved your query.

 

Regards,

Deepika.