Issue with script include

Sarabjeet1
Tera Contributor

Hi,
I want to popup an alert when an incident is opened and if any of the sla attached of that incident has crossed 50%. PFB CS and script include for the same. Not sure where i am going wrong as it is not working.
Incident number field -'number'
Incident no field on incident_sla  table - 'inc_number'

Client script (onload)

function onLoad() {
   
    var incidentSysId = g_form.getUniqueValue();
    var ga = new GlideAjax('CheckSLAStatus');
    ga.addParam('sysparm_name', 'getSLAStatus');
    ga.addParam('number', incidentSysId);
   
    ga.getXMLAnswer(function(response) {
        var slaStatus = JSON.parse(response);
       
        // Check if any SLA is crossing 50%
        if (slaStatus && slaStatus.length > 0) {
            for (var i = 0; i < slaStatus.length; i++) {
                if (slaStatus[i].percentage > 50) {
                    // Display alert message
                    alert('Warning: SLA "' + slaStatus[i].slaName + '" is crossing 50%.');
                    break; // Alert only once if any SLA crosses 50%
                }
            }
        }
    });
}
SCRIPT INCLUDE (client callable)
var CheckSLAStatus = Class.create();
CheckSLAStatus.prototype = {
    initialize: function() {},

    getSLAStatus: function() {
        var incidentSysId = this.getParameter('incidentSysId');
        var slaStatusArray = [];
var slaGR = new GlideRecord('incident_sla');
slaGR.addQuery('inc_number', incidentSysId);
slaGR.query();

while (slaGR.next()) {
    var slaName = slaGR.sla.getDisplayValue();
    var percentage = (slaGR.total_time.getDisplayValue() / slaGR.due.getDisplayValue()) * 100;

    slaStatusArray.push({
        slaName: slaName,
        percentage: percentage
    });
        }

        return JSON.stringify(slaStatusArray);
    },

    type: 'CheckSLAStatus'
};
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Your Script Include is not actually Client callable as the instantiation needs to include an extension of the Ajax processor.  When you have an existing script, then check the box, it doesn't always add the bits it needs.  You'll also need to retrieve the parameter using the same name as the client script

var CheckSLAStatus = Class.create();
CheckSLAStatus.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    getSLAStatus: function() {
        var incidentSysId = this.getParameter('number');
        var slaStatusArray = [];
        var slaGR = new GlideRecord('incident_sla');
        slaGR.addQuery('inc_number', incidentSysId);
        slaGR.query();
        while (slaGR.next()) {
            var slaName = slaGR.sla.getDisplayValue();
            var percentage = (slaGR.total_time.getDisplayValue() / slaGR.due.getDisplayValue()) * 100;

            slaStatusArray.push({
                slaName: slaName,
                percentage: percentage
            });
        }

        return JSON.stringify(slaStatusArray);
    },
    type: 'CheckSLAStatus'
});

If you are still not getting expected results, add some gs.info lines to the Script Include to confirm the value passed in from the client, any records retrieved by the GlideRecord, and the values pushed to the array.

View solution in original post

7 REPLIES 7

Abhay Kumar1
Giga Sage

@Sarabjeet1 By looking into your script i have found below issues:

Parameter Mismatch: In the client script, you are passing the parameter as 'number', but in the Script Include, you are trying to access 'incidentSysId'. This mismatch will result in the Script Include not receiving the correct value.

Incorrect Field Names: In the incident_sla table, you are using 'inc_number', but the correct field that references the Incident is task. The task field stores the sys_id of the associated task (Incident, Change, etc.).

Division of Date/Time Fields: You are trying to divide two date/time fields (total_time and due). These fields are GlideDateTime objects, and you cannot directly divide them. You need to convert these to numeric values (e.g., using .getNumericValue()) for calculation.

Please review again your code, hope this will help you.

SachinPol
Tera Contributor

You Can Write On Load Client Script  given below:

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    var incNo = g_form.getValue('number');
   
    var ga = new GlideAjax('sal_fifty_percentage');
    ga.addParam('sysparm_name', 'slaInfo');
    ga.addParam('sysparm_incNumber', incNo);
    ga.getXML(slaInfoCall);

    function slaInfoCall(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        //g_form.addInfoMessage('test response answer : ' + answer);
        var slaNames = JSON.parse(answer);
        //g_form.addInfoMessage('test response' + slaNames[0]);
        for (var i = 0; i < slaNames.length; i++) {
            g_form.addInfoMessage(slaNames[i]);
        }

    }

}

and script include given below :

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

    slaInfo: function() {
        var incNo = this.getParameter('sysparm_incNumber');
       
        var slaNaes = [];

        var incGr = new GlideRecord('incident');
        incGr.addQuery('number', incNo);
        incGr.query();
        if (incGr.next()) {
            var taskGr = new GlideRecord('task_sla');
            taskGr.addQuery('task', incGr.sys_id);
            taskGr.query();
            while (taskGr.next()) {
                if (taskGr.percentage >= 50) {
                    slaNaes.push(taskGr.sla.name.toString());
                }
            }
            return JSON.stringify(slaNaes);
        }
    },

    type: 'sal_fifty_percentage'
});




SachinPol
Tera Contributor
Given Below is onload client script :
function onLoad() {
    //Type appropriate comment here, and begin script below

    var incNo = g_form.getValue('number');
   
    var ga = new GlideAjax('sal_fifty_percentage');
    ga.addParam('sysparm_name', 'slaInfo');
    ga.addParam('sysparm_incNumber', incNo);
    ga.getXML(slaInfoCall);

    function slaInfoCall(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        //g_form.addInfoMessage('test response answer : ' + answer);
        var slaNames = JSON.parse(answer);
        //g_form.addInfoMessage('test response' + slaNames[0]);
        for (var i = 0; i < slaNames.length; i++) {
            g_form.addInfoMessage(slaNames[i]);
        }

    }

}

and Given Below is Script Include for same :

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

    slaInfo: function() {
        var incNo = this.getParameter('sysparm_incNumber');
       
        var slaNaes = [];

        var incGr = new GlideRecord('incident');
        incGr.addQuery('number', incNo);
        incGr.query();
        if (incGr.next()) {
            var taskGr = new GlideRecord('task_sla');
            taskGr.addQuery('task', incGr.sys_id);
            taskGr.query();
            while (taskGr.next()) {
                if (taskGr.percentage >= 50) {
                    slaNaes.push(taskGr.sla.name.toString());
                }
            }
            return JSON.stringify(slaNaes);
        }
    },

    type: 'sal_fifty_percentage'
});