Sys ID still showing instead of display value (on reference field) - why?

jas101
Tera Expert

Hi, we are using the Service (business_service) selected on a CHG form to auto-set other CHG field values where possible.

This is working great except for one field. On the Service record there is a reference field which references a custom table 'Technology reference'. This shows fine on the Service record but when using a client script to set the 'Category' value on the CHG form with this 'Technology reference' value, the sys ID shows. The correct field on the Technology reference table (u_technology_reference) is the Display value. Client script below. Any help much appreciated.

Thank-you,

DS

 

 

var bs = g_form.getReference('business_service');
 
 g_form.setValue('u_region', bs.u_region);
 g_form.setValue('u_importance', bs.busines_criticality);
 g_form.setValue('u_gpms_relationship', bs.u_gpms_relationship);
 g_form.setValue('u_end_user_count', bs.u_users_count);
 g_form.setValue('u_maintenance_schedule', bs.schedule);
 g_form.setValue('category', bs.u_technology_reference);
 
1 ACCEPTED SOLUTION

benn23
ServiceNow Employee
ServiceNow Employee

Try a difference approach.

Client Script:

 

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

var ga = new GlideAjax("doYourThing");
ga.addParam("sysparm_name","pullStuff");
ga.addParam("sysparm_refId",g_form.getValue('business_service'));
ga.getXML(ajaxResponse);
}

function ajaxResponse(serverResponse) {
// get result element and attributes
var result = serverResponse.responseXML.getElementsByTagName("result");
g_form.setValue('u_region',result[0].getAttribute("region"));
g_form.setValue('u_importance',result[0].getAttribute("importance"));
g_form.setValue('u_gpms_relationship',result[0].getAttribute("gpms_relationship"));
g_form.setValue('u_end_user_count',result[0].getAttribute("end_user_count"));
g_form.setValue('u_maintenance_schedule',result[0].getAttribute("maintenance_schedule"));
g_form.setValue('category',result[0].getAttribute("category"));
}

 

Script Include:

Name: doYourThing

Client Callable: True

Script:

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

pullStuff: function(){
var result = this.newItem("result");
var busId = this.getParameter('sysparm_refId');
var gr = new GlideRecord('cmdb_ci_service');
gr.get(busId);
result.setAttribute('region',gr.u_region);
result.setAttribute('importance',gr.business_criticality);
result.setAttribute('gpms_relationship',gr.u_gpms_relationship);
result.setAttribute('end_user_count',gr.u_users_count);
result.setAttribute('maintenance_schedule',gr.schedule);
result.setAttribute('category',gr.u_technology_reference.getDisplayValue());

},
type: 'doYourThing'
});

View solution in original post

9 REPLIES 9

reg_1979
Tera Guru

Hi DS,

Is the sys_id displaying on the form itself after being selected or in the lookup list when you are picking you value?

If it is in the lookup list, it could be an issue with the list view for the "technology_reference" table.

Some things you can look for/try:

Clearing the cache (cache.do)

If the custom table is inherited, you can see if there is a display value on the parent table, maybe that is causing a conflict?

Hi, the sys ID is showing in the correct/expected 'Category' field on the CHG form after a 'Service' is selected i.e. it is being populated with Service.Technology reference which we want (but we want the display value not the sys ID).

 'Category' is currently just a string field.

I am not able to change this field to a reference field but having just created a new custom 'Category' field which references the custom table (and updating the client script to set the value on this new field) it is now working - great! But to double check, is it correct that I have no option but to use this new custom 'Category' field, i.e. I can't convert the existing Category to a reference?

Thanks again.

benn23
ServiceNow Employee
ServiceNow Employee

Try a difference approach.

Client Script:

 

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

var ga = new GlideAjax("doYourThing");
ga.addParam("sysparm_name","pullStuff");
ga.addParam("sysparm_refId",g_form.getValue('business_service'));
ga.getXML(ajaxResponse);
}

function ajaxResponse(serverResponse) {
// get result element and attributes
var result = serverResponse.responseXML.getElementsByTagName("result");
g_form.setValue('u_region',result[0].getAttribute("region"));
g_form.setValue('u_importance',result[0].getAttribute("importance"));
g_form.setValue('u_gpms_relationship',result[0].getAttribute("gpms_relationship"));
g_form.setValue('u_end_user_count',result[0].getAttribute("end_user_count"));
g_form.setValue('u_maintenance_schedule',result[0].getAttribute("maintenance_schedule"));
g_form.setValue('category',result[0].getAttribute("category"));
}

 

Script Include:

Name: doYourThing

Client Callable: True

Script:

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

pullStuff: function(){
var result = this.newItem("result");
var busId = this.getParameter('sysparm_refId');
var gr = new GlideRecord('cmdb_ci_service');
gr.get(busId);
result.setAttribute('region',gr.u_region);
result.setAttribute('importance',gr.business_criticality);
result.setAttribute('gpms_relationship',gr.u_gpms_relationship);
result.setAttribute('end_user_count',gr.u_users_count);
result.setAttribute('maintenance_schedule',gr.schedule);
result.setAttribute('category',gr.u_technology_reference.getDisplayValue());

},
type: 'doYourThing'
});

Hi, this is really appreciated. Thank-you.

Using this method the display value does show on the existing string Category field now - although now I am wondering if it makes more sense to have this as a custom reference field for easier reporting/filtering etc.

Is using the script include/client script advantageous to just using the client script for other reasons btw?

While talking of script includes, if you could possibly review my post under this thread in case you're able to shed any light on that issue, that would be great.

https://community.servicenow.com/community?id=community_question&sys_id=38c40be9dbd8dbc01dcaf3231f9619e3

I need to get more familiar with script includes.