How to get display value of a reference field?

Yaalini Sakthiv
Giga Contributor

I have a field in my catalog form called 'Asset tag' which is a reference field when we choose any value in 'Asset tag' the field called 'Model' is in "Asset tag's reference table' the model value from that field should get autopopulated to my catalog form 'Model name field'. The 'model' field in Asset tag's reference table is also a reference field when I try to autopopulate it gets the sys id of the model record. Now I don't need the sys id I require the name of the model.The script I used is,

function onChange(control, oldValue, newValue, isLoading) {
   var asset_tag = g_form.getReference('asset_tag_add', getModel); //getModel is our callback function
}
function getModel(asset_tag) { //reference is passed into callback as first arguments
   g_form.setValue('model', asset_tag.model);
}

The above script gives the sysid i need display value of that field

Can anyone help with this?

7 REPLIES 7

Mohith Devatte
Tera Sage
Tera Sage

Hello,

I think in get reference you can do only single level dot-walking which you did above .

But accessing the name of the asset would be double level dot walk which is not possible .

You have to create a script include for this and call it in an on change client script and populate the  value on the form

Please accept my answer if it helps you

@Yaalini Sakthivel  please see the code below for reference

Client script: You need to write on change client script on Asset tag field

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

	var mdl =  new GlideAjax('getModel');
	mdl.addParam('sysparm_name','getModelValue');
	mdl.addParam('sysparm_assettag',newValue);
	mdl.getXML(setModel);

}


function setModel(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('model_field back end name',answer);
}



Script include :

Name :getModel -- >please maek sure Script include name is this

 

var getModel = Class.create();
getModel.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getModelValue:function()
	{
		var md = new GlideRecord('alm_asset');
		md.addQuery('sys_id',this.getParameter('sysparm_assettag'));
		md.query();
		if(md.next())
			{
				return md.model.display_name.toString();
			}
	},
    type: 'getModel'
});

Please mark my answer correct if it helps you

Tudor
Tera Guru

Hello Yaalini,

Option A: you convert the current script from using getReference to GlideAjax:

https://docs.servicenow.com/en-US/bundle/sandiego-application-development/page/script/ajax/topic/p_AJAX.html

Option B: I haven't tested this one.

You create a hidden variable that stores the model which you populate with the above script and you add an additional getReference which gets you the info.

I hope this helps!

 Tudor

@Tudor please see the code below for reference

Client script:

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

	var grp =  new GlideAjax('getModel');
	grp.addParam('sysparm_name','getModelValue');
	grp.addParam('sysparm_assettag',newValue);
	grp.getXML(setModel);

}


function setModel(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");
		g_form.setValue('model_field back end name',answer);
}



Script include :

Name :getModel -- >please maek sure Script include name is this

 

var getModel = Class.create();
getModel.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getModelValue:function()
	{
		var md = new GlideRecord('alm_asset');
		md.addQuery('sys_id',this.getParameter('sysparm_assettag'));
		md.query();
		if(md.next())
			{
				return md.model.display_name.toString();
			}
	},
    type: 'getModel'
});