Issue with the client script or script include.

Spike236
Tera Contributor

Hello folks,

 

i have created a catalog client script and script include to count related items for the value on the picked variable and based on the number, make variable2 visible or not. I have changed the script include to return number two for testing purposes, however its returning null. That makes me to believe that there is an issue with the logic. However,

I dont know where the issue is. Grateful for any help!

 

Script include

 

var Utils = Class.create();
Utils .prototype = {
    initialize: function() {},
    getCount: function() {
        var pSysId = this.getParameter('sysparm_SysId');

        var progGR = new GlideRecord('table');
        progGR .addQuery('port', pSysId );
        progGR .addQuery('pro.active', true);
        progGR .addQuery('prote', gs.getProperty('property_name'));
        progGR .setLimit(2);
        progGR .query();
        return '2'; //progGR .getRowCount();
    },
    type: 'Utils '
};

 

catalog client script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var UtilsAJAX = new GlideAjax('utils');
    UtilsAJAX .addParam('sysparm_name', 'getCount');
    UtilsAJAX .addParam('sysparm_SysId', g_form.getValue('variable1'));
    UtilsAJAX.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === '2') {
            g_form.setDisplay('prog', true);
        } else {
            g_form.setDisplay('prog', false);
        }

        g_form.addInfoMessage(answer);
    });
}

 

 

1 ACCEPTED SOLUTION

Marco0o1
Tera Sage

Hi @Spike236 ,

 

That looks you dont create you element ass Client callable, if you dont select that tag you cant call your script include from a script, first you need to tag the client callable and make some changes:

Marco0o1_0-1694460034605.png

 

var Utils = Class.create();

Utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCount: function() {
        var pSysId = this.getParameter('sysparm_SysId');

        var progGR = new GlideRecord('table');
        progGR .addQuery('port', pSysId );
        progGR .addQuery('pro.active', true);
        progGR .addQuery('prote', gs.getProperty('property_name'));
        progGR .setLimit(2);
        progGR .query();
        return '2'; //progGR .getRowCount();
    },
    type: 'Utils '
});

 

Then you call your function, just make sure is called with the exactly same name you created in your script include:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var UtilsAJAX = new GlideAjax('Utils'); //Make sure is writing in the same method you create in your Script Inclide, capital letters are diferents
    UtilsAJAX .addParam('sysparm_name', 'getCount');
    UtilsAJAX .addParam('sysparm_SysId', g_form.getValue('variable1'));
    UtilsAJAX.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === '2') {
            g_form.setDisplay('prog', true);
        } else {
            g_form.setDisplay('prog', false);
        }

        g_form.addInfoMessage(answer);
    });
}

 

Hope that help you

View solution in original post

5 REPLIES 5

Marco0o1
Tera Sage

Hi @Spike236 ,

 

That looks you dont create you element ass Client callable, if you dont select that tag you cant call your script include from a script, first you need to tag the client callable and make some changes:

Marco0o1_0-1694460034605.png

 

var Utils = Class.create();

Utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCount: function() {
        var pSysId = this.getParameter('sysparm_SysId');

        var progGR = new GlideRecord('table');
        progGR .addQuery('port', pSysId );
        progGR .addQuery('pro.active', true);
        progGR .addQuery('prote', gs.getProperty('property_name'));
        progGR .setLimit(2);
        progGR .query();
        return '2'; //progGR .getRowCount();
    },
    type: 'Utils '
});

 

Then you call your function, just make sure is called with the exactly same name you created in your script include:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var UtilsAJAX = new GlideAjax('Utils'); //Make sure is writing in the same method you create in your Script Inclide, capital letters are diferents
    UtilsAJAX .addParam('sysparm_name', 'getCount');
    UtilsAJAX .addParam('sysparm_SysId', g_form.getValue('variable1'));
    UtilsAJAX.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === '2') {
            g_form.setDisplay('prog', true);
        } else {
            g_form.setDisplay('prog', false);
        }

        g_form.addInfoMessage(answer);
    });
}

 

Hope that help you

Hi, thank you for reply. It is set as client callable,not sure why its not working. Even if I commented most of the script include it was not working.

You declare correctly your object?

 

Utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

And called the script include in your script correctly?

 var UtilsAJAX = new GlideAjax('Utils'); 

Vishal Birajdar
Giga Sage

Hi @Spike236 

 

The code you have written looks good.

You just need to make script include as client callable but you have already saved it without client callable as checked.

 

Now if you try to make it client callable sometime it won't work so my suggestion will be create new client  callable script include

and it will work.

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates