How to get the display name of reference field if using g_form.getReference

Priyanka132
Giga Contributor

Hi All,

I am using g_form.getReference to populate users details in a catalog item via client script. For a few fields in user profile like Business Unit, Department, cost center, I am getting the sys_id instead of the display value.

Since these fields are reference field , the value returned is sys id. How can i get the display value instead of the sys id.

 

find_real_file.png

 

var user= g_form.getReference('requested_for');
g_form.setValue('business_unit',user.u_business_unit.u_name);    ///business unit is a reference field on user table

 

I understand i can use asynchronous glide record in the client script to populate. but not sure if that is recommended

 

 

 

 

 

1 ACCEPTED SOLUTION

ArunKRam
Giga Expert

Hi Priyanka,

I think the best way to achieve this is by using Glide Ajax. Send the sysid to the script include, query the tables and return all the display values in an array.

Thanks,

Arun

View solution in original post

12 REPLIES 12

Yes I agree. Please use GlideAjax instead of getReference.

ravichanduk
Mega Expert

try this with script

g_form.setValue('business_unit',user.u_business_unit.u_name.toString()); 

Wirasat
Tera Guru

There are 2 ways you can achieve this.

Use getReference function in client script. Here is the script that I used.

 

var obj= g_form.getReference('requested_for', getEmail);

function getEmail(obj) {
if (obj != '') {
g_form.setValue('Email_Address', obj.email);
}
}

-------------------------

Another way would be to use GlideAjax call in client script and use JSON object to return the data back to calling client script. Here is how you can achieve that.

Client Script

---------------

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

var userData = new GlideAjax('GetUserData');

userData.addParam('sysparm_name','getUserInfo');
// Pass the Requested for sys_id
userData.addParam('sysparm_userid', g_form.getValue('userid'));
// Send the request to the server
userData.getXML(populateUserData);


// When the response is back from the server
function populateUserData(response){

var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); //JSON String

//Convert JSON string to JSON object
answer=JSON.parse(answer);


//Populate the user data values
g_form.clearValue('u_first_name');
g_form.setValue('u_first_name', answer.firstName);
g_form.clearValue('u_last_name');
g_form.setValue('u_last_name', answer.lastName);
g_form.clearValue('u_email');
g_form.setValue('u_email', answer.email);
}

}

Script Include

-----------------

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

getUserInfo: function() {
var X = new GlideRecord("sys_user");
//Get one row of data based on sys_id
X.get(this.getParameter('sysparm_userid'));

var userdata = {};
userdata.firstName=X.getValue('first_name');
userdata.lastName=X.getValue('last_name');
userdata.email=X.getValue('email');


var jsonData=JSON.stringify(userdata);

return jsonData;


},

type: 'GetUserData'
});