Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to convert sysid to display value in the catalog form

Naresh291
Tera Contributor

Hi ,

I have a client script on the catalog form . There is a field called 'division' in the form which should autopopulate the value of 'division' field also present in the user table  according to the  requested for. I have a script include where i have fetched the values from user table and i have called the script include from client script . The problem is now i get a sysid instead of display value . Even if i use toString() or getDisplayValue still it gives me the sysid instead of displayvalue .

Here is the line of code in script include:

result.setAttribute('division', gr.u_division);

where gr is the glideRecord of sys_user table and the division field in the user table is a reference field refering to division table .

Client script:

.

.

ga.getXML(employeeInfo);

function employeeInfo(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
var div = result[0].getAttribute("division");

g_form.setValue("division",div );
}

 

Any leads on this is highly appreciated

 

Regards,

Naresh

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can get the display value of reference field in the script include function

result.setAttribute('division', gr.u_division.getDisplayValue());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Dipin4
Tera Contributor

Hi,

In your server script include from where you are getting the value using GlideAjax, instead of using 
gr.getValue('field_name'); use gr.getDisplayValue('field_name');

Please mark it as helpful/correct answer if it solves your query.

 

thanks.

Dipin4
Tera Contributor

Server script: Client callable Script include

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


getUserData : function()
{
var con_sysID = this.getParameter("sysparm_con_sys_id"); //sys_id from client side, requested for in your case

var user= new GlideRecord('sys_user');
if (user.get(con_sysID)) {

var con_obj ={};
con_obj.name = user.getDisplayValue('field_name');
con_obj.email = user.getValue('email');


return JSON.stringify(con_obj);
}
},

type: 'CSMDataUtils'
});

Client script:

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

if (newValue) {
if (newValue != oldValue) {
var asset_det = new GlideAjax('CSMDataUtils');
asset_det.addParam('sysparm_name', 'getUserData');
asset_det.addParam('sysparm_con_sys_id', g_form.getValue('requested_for')); //make field name correction accordingly
asset_det.getXML(UserDetails);
}
}
}

function UserDetails(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var result = JSON.parse(answer);

g_form.setValue('division', result.name);
g_form.setValue('email', result.email);

}

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

You can get the display value of reference field in the script include function

result.setAttribute('division', gr.u_division.getDisplayValue());

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Is there any way of retriveing the display value in the client script rather than in the script include ?