- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2024 10:48 AM
Good Day, I need help with a client script.
I created a field called 'SLA Breached Reason' to display and be mandatory when an Incident State is changed to close and Has Breached is true. I can't get the Task_SLA field through incidents through UI Policy so I need a script. Any ideas? Regards
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2024 12:04 PM - edited ‎02-23-2024 02:26 PM
Hi @Sherridan Maitl ,
You can On change Create a client script and script include as shown below,
Script Include: client callable
var checksla = Class.create();
checksla.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getSlaDetails: function() {
var taskSlaSysId = this.getParameter('sysparm_sysid');
var taskSla = new GlideRecord('task_sla');
taskSla.addQuery('task', taskSlaSysId.toString());
taskSla.query();
if (taskSla.next()) {
gs.info('inside if' + taskSlaSysId);
if (taskSla.has_breached == true) {
return true;
} else if (taskSla.has_breached == false || taskSla.has_breached == "false") {
return false;
}
}
},
type: 'checksla'
});
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
// g_form.setMandatory('u_sla_breached_reason', false);
return;
}
g_form.setVisible('u_sla_breached_reason', false); // everywhere replace u_sla_breached_reason with "SLA Breached Reason" backend value
if (newValue == '7') {
var sysid = g_form.getUniqueValue();
var slaTask = new GlideAjax('checksla');
slaTask.addParam('sysparm_name', 'getSlaDetails');
slaTask.addParam('sysparm_sysid', sysid);
slaTask.getXML(getresponse);
} else {
g_form.setMandatory('u_sla_breached_reason', false);
g_form.setVisible('u_sla_breached_reason', false);
}
function getresponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer == true || answer == 'true') {
g_form.setVisible('u_sla_breached_reason', true);
g_form.setMandatory('u_sla_breached_reason', true);
}
}
//Type appropriate comment here, and begin script below
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2024 11:18 AM
Hi,
you could create a onChange Client Script that runs when State is changed to "closed" and have a GlideAjax call to a script include to check if task_sla is true.
Here is an example:
Script Include:
var CheckSLABreached = Class.create();
CheckSLABreached.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasBreachedSLA: function() {
var taskSysId = this.getParameter('sysparm_task_sys_id');
var taskSLAgr = new GlideRecord('task_sla');
taskSLAgr.addQuery('task', taskSysId);
taskSLAgr.query();
while (taskSLAgr.next()) {
if (taskSLAgr.has_breached == 'true') {
return true;
}
}
return false;
},
type: 'CheckSLABreached'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var stateClosedValue = '7'; // Replace with the actual value for 'Closed' in your instance
if (newValue == stateClosedValue) {
// Make the GlideAjax call to check for SLA breach
var ga = new GlideAjax('CheckSLABreached');
ga.addParam('sysparm_name', 'hasBreachedSLA');
ga.addParam('sysparm_task_sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(checkForSLABreach);
}
}
function checkForSLABreach(answer) {
g_form.setMandatory('sla_breached_reason', answer);
}
I hope this could help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2024 12:04 PM - edited ‎02-23-2024 02:26 PM
Hi @Sherridan Maitl ,
You can On change Create a client script and script include as shown below,
Script Include: client callable
var checksla = Class.create();
checksla.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getSlaDetails: function() {
var taskSlaSysId = this.getParameter('sysparm_sysid');
var taskSla = new GlideRecord('task_sla');
taskSla.addQuery('task', taskSlaSysId.toString());
taskSla.query();
if (taskSla.next()) {
gs.info('inside if' + taskSlaSysId);
if (taskSla.has_breached == true) {
return true;
} else if (taskSla.has_breached == false || taskSla.has_breached == "false") {
return false;
}
}
},
type: 'checksla'
});
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (newValue === '') {
// g_form.setMandatory('u_sla_breached_reason', false);
return;
}
g_form.setVisible('u_sla_breached_reason', false); // everywhere replace u_sla_breached_reason with "SLA Breached Reason" backend value
if (newValue == '7') {
var sysid = g_form.getUniqueValue();
var slaTask = new GlideAjax('checksla');
slaTask.addParam('sysparm_name', 'getSlaDetails');
slaTask.addParam('sysparm_sysid', sysid);
slaTask.getXML(getresponse);
} else {
g_form.setMandatory('u_sla_breached_reason', false);
g_form.setVisible('u_sla_breached_reason', false);
}
function getresponse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer == true || answer == 'true') {
g_form.setVisible('u_sla_breached_reason', true);
g_form.setMandatory('u_sla_breached_reason', true);
}
}
//Type appropriate comment here, and begin script below
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 01:12 PM
Thanks! This is a great I will update after I'm finished testing