- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2015 01:59 PM
What is the best method for setting a field like (Reason for SLA breach) as mandatory on an Incident when users try to set State as Resolved when the SLA is breached. I tried adding a UI Policy with the below code but could not get any result
function onCondition() {
var sla = new GlideRecord('task_sla')
sla.addQuery('task', 'current.number');
sla.addQuery('has_breached', 'true');
sla.query();
while(sla.hasNext()){
if(current.state==6)
answer=true;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2015 04:39 PM
Hello Vijay,
I am guessing "Reason for SLA breach" is a custom field on incident table.I would suggest to write a business rule and add error message ( to fill in reason for breach) plus abort the action if the field is empty. Let me know if the below works or you have specific requirement to make field look mandatory, as the cod that you have written is not following the best practices of code writting which ServiceNow suggest. We do not use Gliderecord and addquery on client side scripts.
Table : Incident
When : before
On: Update
Condition : current.u_reason_for_breach.nil() && current.state.changesTo(6)
var sla = new GlideRecord('task_sla')
sla.addQuery('task', current.sys_id);
sla.addQuery('has_breached', 'true');
sla.query();
while(sla.Next()){
gs.addErrorMessage('Please fill in the field reason for breach');
current.setAbortAction(true);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2015 09:21 PM
The actual process on any form incident,change,problem etc state field is not editable by any one. It should change the state by using Ui buttons.
In your case state field is ediatble manually then you can go for on submit script and do the same code.
var sla = new GlideRecord('task_sla')
sla.addQuery('task', g_form.getUniqueValue());
sla.addQuery('has_breached', 'true');
sla.query();
while(sla.hasNext())
{
alert('Please fill in the field reason for breach');
g_form.setMandatory('reason_to_breach', true);
return false;
}
instead of of writing glide record in client script call script include is best practice.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2015 04:33 PM
Thank you Harish and Neeraj, Added the below to the business rule and it is working. When I had abort action this system kep on adding the error message. This should solve my requirement. Appreciate the time
var sla = new GlideRecord('task_sla');
sla.addQuery('task', current.sys_id);
sla.addQuery('has_breached', 'true');
sla.query();
while(sla.hasNext())
{
gs.addErrorMessage('Please fill in the field Reason for SLA breach:');
break;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2017 10:37 AM
Hi all,
I came across this thread when having the same issue myself and created a solution which works really well for me (note I have 1 SLA per incident).
1st - we created a new field called "SLA Breach Reason" on the incident form. (string(40) for me)
2nd - we created a tickbox on the incident form called "SLA Breached" (default false)
3rd - we created a business rule which keeps the newly created tickbox (SLA Breached) in sync with the state with the "has breached" flag on the task table:
table: task_sla
order: 100
Update: true
when: after
conditions:
has breached - changes
and the advanced script:
(function executeRule(current, previous /*null when async*/) {
if (current.task.sys_class_name=='incident') {
var gr = new GlideRecord('incident');
gr.get(current.task);
gr.u_sla_breached=current.has_breached;
gr.update();
}
})(current, previous);
4th we created a UI policy in the incident table
Conditions: SLA Breach = true (you could add in incident state however I wanted it mandatory as soon as SLA breached!)
UI Actions: SLA Breach Reason is "Mandatory" and "Visible"
Hope this is helpful to others.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2017 08:58 AM
Hello Dan,
have you ever tried to do this for 2 SLA like time to react and time to close?
Thank you
Sniega