Get company name instead company Sys_id

Hapuertam
Kilo Contributor

Hi everyone,

i need to populate a text field (company) from an user reference field that have selected previously, actually with the next script im getting the company sys_id but i cant find the way to fill the text field with company name.

 

this is the code...

 

function onChange(control, oldValue, newValue, isLoading) {
var id = g_form.getValue('user_sol');

var user = new GlideRecord('sys_user');

user.addQuery('sys_id',id);


user.query();


if (user.get(newValue)){

g_form.setValue('Prueba_cargo',user.title);
g_form.setValue('area_sol',user.company);

}
}

 

thanks in advance

1 ACCEPTED SOLUTION

Nate23
Mega Guru

Not sure why you wouldn't want to use a reference to the core_company table in this scenario would save heart ache, but either way the best practice for this would be to use a GlideAjax Call because you can return only the data you need, because doing a GlideRecord Query in a client script rely's on the end users browser to perform the query resulting in using more of their resources and getReference() with a call back is okay, but it will return the entire record and like you are experiencing it still will not get you the name. you could possibly use getDisplayValue() in the call back to remediate that issue but I have not tested it. needless to say here is how I would solution it if you want the display values:

 

Client script:

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	var ga = new GlideAjax('ServiceCatalogClientUtil');//call Script Include
	ga.addParam('sysparm_name','getDisplayValues');//Call Function
	ga.addParam('sysparm_record',newValue);//send record sys_id
	ga.addParam('sysparm_table', 'sys_user');
	ga.addParam('sysparm_fields', 'company,title');
	ga.getXML(getCompanyName);
	
}

function getCompanyName(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var displayValues = answer.split(',');
	alert(answer);
	g_form.setValue('area_sol', displayValues[0]+'');
	g_form.setValue('Prueba_cargo', displayValues[1]+'');
}

 

Script Include:

Name: ServiceCatalogClientUtil

Client Callable: true

var ServiceCatalogClientUtil = Class.create();
ServiceCatalogClientUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getDisplayValues: function(){
		try{
		var table = this.getParameter('sysparm_table');
		var recordID = this.getParameter('sysparm_record');
		var fields = this.getParameter('sysparm_fields').split(',');
		var displayValues = [];
		
		var gr = new GlideRecord(table);
		if(gr.get(recordID)){
			for(var i=0;fields.length>i;i++){
				displayValues.push(gr[fields[i]].getDisplayValue());
			}
		}
		return displayValues.toString();
		}catch(e){
			gs.log('getDisplayValues: '+e);
		}
	},
    type: 'ServiceCatalogClientUtil'
});

This will have better performance and lets you reuse this for future cases. Also, you can add more functionality to this Util Script Include for any other future client side enhancements.

 

Cheers,

Nate

View solution in original post

9 REPLIES 9

Nitin_NOW
Tera Guru

Please use the below script which should satisfy your requirement.

function onChange(control, oldValue, newValue, isLoading) {
if(isLoading)
return;
var id = g_form.getReference('user_sol',test); }

function test(id){
g_form.setValue('area_sol', id.company);

}

Please change the field values accordingly 

Please hit correct based on impact of response.

Thanks!

Im sorry, i have a question... what do u men with test variable using in getreference function? thanks!

Hi!

 

Thanks for answer..

 

Actually im using this script:

function onChange(control, oldValue, newValue, isLoading) {
if(isLoading)
return;
var id = g_form.getReference('user_sol',test); }

function test(id){
g_form.setValue('area_sol', id.company);

}

 

im trying to set the name but aparently still having the company sys_id, how can i access to this attribute?

maybe ussing something like id.company.name? (this is not working) any suggestion?

 

thanks!

Hapuertam
Kilo Contributor

 a