- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 04:32 AM
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)
SCRIPT INCLUDE (client callable)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 05:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 05:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2024 04:05 PM
Hi @Sarabjeet1
I hope you are doing well!
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'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 06:20 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 06:39 PM
Thanks for your response @Sarabjeet1 , Could you please share me your updated script ? I will review it. Thank you!