Reason for SLA breach validation

vyjayanth
Giga Expert

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;

  }

}

1 ACCEPTED SOLUTION

Neeraj Gupta1
Mega Expert

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);


  }


}


View solution in original post

8 REPLIES 8

Neeraj Gupta1
Mega Expert

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);


  }


}


Harish Murikina
Tera Guru

Hi Vijay,



                My thought on this.



In resolved ui action write the code either SLA breached or not if its give alert message and make field mandatory.




  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;


}



Make sure your ui action is client callable


Hi Neeraj, I tried this as a UI action and it works fine, but it does not work when i have it in a Business Rule or client script. Or to say when users tries to perform form update. like manually setting the state to Resolved, this field Reason for SLA breach is not becoming mandatory. Please suggest is there a best practice to handle this.


Hello Vijay,



Can you tell me, what error are you getting while doing the business rule. The above business rule would only restrict the user from saving the incident form, unless the user fills in reason for sla breach field. It won't mark the field as mandatory, the error message would should up only when he tries to submit the form.


I am guessing Harish's solution would work only when user hits Resolved UI button. If someone tries to change the manually from dropdown, it won't work.



If you have specific requirement to mark the field mandatory once the state is changed to resolved. I would suggest to write a glideajax. Here:


GlideAjax - ServiceNow Wiki



Cheers !


Neeraj