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

benn23
ServiceNow Employee
ServiceNow Employee

In your getReference, you were pulling back the value of a reference field, which is that referenced record's sys_id:

g_form.setValue('category', bs.u_technology_reference);

Unfortunately you can't dot-walk any further using a getReference:  ex: bs.u_technology_reference.name <-- won't work

 

I'd suggest staying away from getReferences.  They pull back the entire referenced record vs using a script include to only pull back what's needed.  Plus your getReference was a synchronous call.  The ajax method I sent you was asynchronous.  Additionally, using a script include you can create reusable functions. 

Ajax scripts seem tricky at first but once you build out a few you'll get it.  They're game changers. 😉

AJAX

Many thanks indeed for your help/explanation.

Also please can you tell me, what it is about the script include that can allow the display value (on the string field)whereas just the client script cannot? Thanks.

Wow, Daniel, please do not post reply after reply after reply. One reply is sufficient, and you can edit it after it's been posted if you have more to add. 

Tim Woodruff
Mega Guru

First, you should try to always use .getValue() when getting a value from a GlideRecord. More info at http://pbr.sngeek.com/

Second, since you're using .getReference(), you should be using it asynchronously. More info at http://ajax.sngeek.com/

Finally, since you have the GlideRecord, you can try specifying the Display Value in your method call: 

g_form.setValue('category', bs.getValue('u_technology_reference'), br.u_technology_reference.getDisplayValue());