Issue with Script Include function call

Rose17
Tera Contributor

Hi All,

Requirement is based on change of assigned to, I need to display count of incidents assigned to the selected assigned to.

I had used onChange client script and Script include, but I am getting null value.

When I tried to debug using script debugger, logs, the function written inside script include is not getting executed.

I have provide admin role for the execute ACL for the script include, also it is client callable.

Below is the code-

Client script-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    //Type appropriate comment here, and begin script below

    var sc = new GlideAjax('getIncidentCount');
    sc.addParam('sysparm_func', 'getCount');
    sc.addParam('sysparm_value', g_form.getValue('assigned_to'));
    sc.getXML(getResponse);

    function getResponse(response) {
        var name = response.responseXML.documentElement.getAttribute('answer');
        g_form.addInfoMessage(name);
    }
}
 
Script include-
var getIncidentCount = Class.create();
getIncidentCount.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCount: function()
    {
        var count;
        gs.log('inside SI');
        var assign= this.getParamater('sysparm_value');
        var user= new GlideAggregate('incident');
        user.addQuery('assigned_to',assign);
        user.addAggregate('COUNT','assigned_to');
        user.groupBy('assigned_to');
        user.query();
        while(user.next())
        {
            gs.log('inside SI if');
            count= user.getAggregate('COUNT','assigned_to');
            gs.log("inc count is "+count);  
        }
        return count;
    },
    type: 'getIncidentCount'
});
Could someone help me resolve this issue?
@Ankur Bawiskar Your help is highly appreciated.
 
Thanks in advance.

 

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Hi Rose,

Good job on the attempt and logging to troubleshoot.  The function is not getting executed because GlideAjax must use sysparm_name to identify the function

sc.addParam('sysparm_name', 'getCount');

https://docs.servicenow.com/bundle/vancouver-api-reference/page/app-store/dev_portal/API_reference/G... 

Once the function is getting called, if you are still not getting expected results, your logs, and others that you add, will help show where there is a typo or logic/syntax issue.

It is working now. But when I try to display the message in the client script using- 

g_form.addInfoMessage("User " +g_form.getDisplayValue('assigned_to')+ " has " +count+ " assigned incidents");
It is showing the message on the form in this way- User INC0000055 has 9 assigned incidents.
Instead of assigned to name, it is showing incident number.

 

Marcos Kassak
Kilo Sage
Kilo Sage

Hi @Rose17,

 

As Brad pointed out you need to use the sysparm_name to identify your function and I also noticed a typo in the Script Include:

 

Instead of this.getParamater('sysparm_value');, it should be this.getParameter('sysparm_value');

 

Client:

 

 

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

    var sc = new GlideAjax('getIncidentCount');
    sc.addParam('sysparm_name', 'getCount'); // Change 'sysparm_func' to 'sysparm_name'
    sc.addParam('sysparm_value', g_form.getValue('assigned_to'));
    sc.getXML(getResponse);

    function getResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.addInfoMessage('Count of Incidents: ' + answer);
    }
}

 

 

SC:

 

 

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

    getCount: function() {
        var count = 0;
        var assign = this.getParameter('sysparm_value'); // Corrected typo in getParameter
        var user = new GlideAggregate('incident');
        user.addQuery('assigned_to', assign);
        user.addAggregate('COUNT', 'assigned_to');
        user.groupBy('assigned_to');
        user.query();

        while (user.next()) {
            count = user.getAggregate('COUNT', 'assigned_to');
            gs.debug("Incident count is " + count);
        }
        return count;
    },
    type: 'getIncidentCount'
});

 

Also ensure the response handling in your GlideAjax is correct and matches the expected structure from the Script Include.

 

If you found my answer helpful or correct in any way, please don't forget to mark it to help future readers! 👍

 

--

 

Kind regards,


Marcos Kassak
Solution Consultant  🎯

Amit Gujarathi
Giga Sage
Giga Sage

HI @Rose17 ,
I trust you are doing great.
Based on your description, here's the revised Script Include:

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

    getCount: function() {
        var assign = this.getParameter('sysparm_value');
        var agg = new GlideAggregate('incident');
        agg.addQuery('assigned_to', assign);
        agg.addAggregate('COUNT');
        agg.query();
        var count = 0;
        if (agg.next()) {
            count = agg.getAggregate('COUNT');
        }
        gs.log("Incident count for user " + assign + " is " + count);
        return count;
    },

    type: 'getIncidentCount'
});

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi