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

Hapuertam
Kilo Contributor

i want to add something:

 

sys_user.company
Table
sys_user
Field
company
Type
reference
Reference
core_company
Max Length
32

the type of this field is reference, how can i get the name of the other table that is referenced?

 

thanks!

BigMikeyVegas
Tera Guru

would core_company.name not work?

dont get anything 😞

Dylan Mann1
Giga Guru

If you want to avoid creating a script include, here's a potential work around:

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();

   // Get the company record
   var company = new GlideRecord('core_company');
   company.get(user.company);

   if(user.get(newValue)) {
      g_form.setValue('Prueba_cargo',user.title);
      g_form.setValue('area_sol', company.name); 
   }
}

Mark this helpful or correct if it helped you, I appreciate the feedback

Dylan

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