Client script help with integer field

booher04
Tera Guru

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.  

1 ACCEPTED SOLUTION

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

View solution in original post

12 REPLIES 12

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...

Willem
Giga Sage
Giga Sage

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'
});

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"

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'
});

Change

return obj;

to

return JSON.stringify(obj);

on Script Includes