- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 03:34 PM
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
Solved! Go to Solution.
- Labels:
-
Personal Developer Instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 08:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 04:02 PM
i want to add something:
| ||||||||||
|
the type of this field is reference, how can i get the name of the other table that is referenced?
thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 04:08 PM
would core_company.name not work?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 05:41 PM
dont get anything 😞

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 07:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2018 08:40 PM
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