- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 09:56 AM
I have a client script setup to pull in data to 2 fields(integer, reference) from 1 field(reference field). The reference field data is working correctly, however I cannot get the integer field to populate correctly. I've gotten it to display the sys_id but I can't get the correct value of the field to display automatically.
I have an OnChange client script as follows:
onChane of application_name field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var appName = g_form.getReference('application_name', setInfo);
function setInfo(appName){
g_form.setValue('it_owner',appName.managed_by);
g_form.setValue('app_code',appName.u_app_list);
}
}
application_name is a reference to table cmdb_ci_service. The managed_by is a reference field(it's working how it should) which populates to the it_owner reference field and the u_app_list is an integer on the table cmdb_ci_business_app which is not populating to the app_code field in the catalog item. I've tried setting the app_code field to a reference variable, look up select box, single line text.. none have worked.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 12:51 PM
try below
var appHelper = Class.create();
appHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAppData: function() {
var id = this.getParameter('sysparm_appname');
var gr = new GlideRecord('cmdb_ci_service');
if (gr.get(id)) {
var obj = {};
obj.managed_by = gr.managed_by.toString();
obj.u_app_id = gr.u_application_catalog_entry.u_app_id.getDisplayValue();
return JSON.stringify(obj);
}
},
type: 'appHelper'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('appHelper');
ajax.addParam('sysparm_name', 'getAppData');
ajax.addParam('sysparm_appname', g_form.getValue('application_name'));
ajax.getXMLAnswer(function(answer) {
var answerObj = JSON.parse(answer);
g_form.setValue('it_owner', answerObj.managed_by);
g_form.setValue('app_code', answerObj.u_app_id.toString());
});
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 10:27 AM
I will recommend to use Script Includes and GlideAjax to query since .getDispaly Value() will not work on client script.
Refer for example: https://community.servicenow.com/community?id=community_article&sys_id=67cdfcd3db4c985c2be0a851ca961...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 10:28 AM
You need to get the Display value.
For this you will need to call a Client callable script include using GlideAjax.
Can you try this:
(change fields/query accordingly)
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('appHelper');
ajax.addParam('sysparm_name', 'getAppData');
ajax.addParam('sysparm_appname', g_form.getValue('application_name'));
ajax.getXMLAnswer(answerHandler);
function answerHandler(answer) {
var answerObj = JSON.parse(answer);
g_form.setValue('it_owner', answerObj.managed_by);
g_form.setValue('app_code', answerObj.u_app_list.toString());
}
}
Client callable script include:
var appHelper = Class.create();
appHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAppData: function () {
var id = this.getParameter('sysparm_appname');
var gr = new GlideRecord('cmdb_ci_service');
if (gr.get(id)) {
var obj = {};
obj.managed_by = gr.managed_by;
obj.u_app_list = gr.u_app_list.getDisplayValue();
return obj;
}
},
type: 'appHelper'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 11:37 AM
Thank you for the input on this. I created the script include... made it client callable and then updated my client script with the script(I updated my fields) and I'm getting the following error in the portal when I select the application_name choice.
"There is a javascript error in your browser console"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 11:39 AM
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('appHelper');
ajax.addParam('sysparm_name', 'getAppData');
ajax.addParam('sysparm_appname', g_form.getValue('application_name'));
ajax.getXMLAnswer(answerHandler);
function answerHandler(answer) {
var answerObj = JSON.parse(answer);
g_form.setValue('it_owner', answerObj.managed_by);
g_form.setValue('app_code', answerObj.u_app_id.toString());
}
}
Script Include:
var appHelper = Class.create();
appHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAppData: function () {
var id = this.getParameter('sysparm_appname');
var gr = new GlideRecord('cmdb_ci_service');
if (gr.get(id)) {
var obj = {};
obj.managed_by = gr.managed_by;
obj.u_app_id = gr.u_app_id.getDisplayValue();
return obj;
}
},
type: 'appHelper'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2020 11:46 AM
Change
return obj;
to
return JSON.stringify(obj);
on Script Includes