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

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.

AbdulNow
Tera Guru

Hi @Sarabjeet1 

 

I hope you are doing well!

 

I have developed the script according to your requirements, and it is functioning as expected. Please refer to the scripts below as a reference.
 
Client Script on Load
function onLoad() {
    // Prevent the script from running on new records
    if (g_form.isNewRecord()) return;

    // Set up GlideAjax to call the Script Include
    var ga = new GlideAjax('CheckSLAPercentage');
    ga.addParam('sysparm_name', 'checkSLAForTask');
    ga.addParam('sysparm_taskSysId', g_form.getUniqueValue());

    // Make the GlideAjax call
    ga.getAnswer(function(response) {
        if (response === 'true') {
            alert("Warning: An attached SLA has crossed 50% of its duration.");
        }
    });
}

 

 

Script Include

 

var CheckSLAPercentage = Class.create();
CheckSLAPercentage.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    initialize: function() {},

    // This function will be called from the client script
    checkSLAForTask: function(taskSysId) {
        var slaGR = new GlideRecord('task_sla');
        slaGR.addQuery('task', taskSysId); // Get SLAs for this incident
        slaGR.query();
        
        while (slaGR.next()) {
            var percentage = parseInt(slaGR.getValue('percentage_complete'), 10);
            if (percentage >= 50) {
                return 'true'; // SLA has crossed 50%
            }
        }
        return 'false'; // No SLA has crossed 50%
    },

    type: 'CheckSLAPercentage'
};

 

 

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
 
Regards,
Wahid

Hi @AbdulNow 
apologies for the delay in response. But this code is not working for me.
I used the same script, client script on incident table and script  include as it is. But no luck.

Thanks for your response @Sarabjeet1 , Could you please share me your updated script ? I will review it. Thank you!