Make fields mandatory when SLA breached on incident form

priya vishwaka1
Tera Contributor

Hello All ,

I have one field 'sla breached reason' field on incident field. this has to visible and mandatory when "SLA breached and Incident state changes to resolved"

If sla not breached this field doesn't want to show.

kindly help me the code for this?(client scrpt+script include only)

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use Display business rule on incident table and check if the SLA is breached for this incident and store it as flag in g_scratchpad variable

then use onLoad client script to hide it

BR:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var gr = new GlideRecord("task_sla");
	gr.addQuery("task", current.getUniqueValue());
	gr.addEncodedQuery("has_breached=true");
	gr.query();
	g_scratchpad.isBreached = gr.hasNext();

})(current, previous);

Client Script:

function onLoad(){

	// hide it
	g_form.setDisplay('u_sla_breached_reason', false);

	// show and make mandatory if breached
	if(g_scratchpad.isBreached.toString() == 'true'){
		g_form.setDisplay('u_sla_breached_reason', true);
		g_form.setMandatory('u_sla_breached_reason', true);
	}

}

OR

You can use onLoad client script + GlideAjax as well

GlideAjax Example Cheat Sheet

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@priya vishwakarma 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thank you so much. This worked awesome!

Harish KM
Kilo Patron
Kilo Patron

Hi you can write client script and Script include like this

Script Include:

checkSLA: function()
{
var getRecordSysID  = this.getParameter("sysparm_id");

var sla = new GlideRecord('task_sla')

sla.addQuery('task', getRecordSysID);

sla.addQuery('has_breached', 'true');

sla.query();

while(sla.Next()){

return true;

  }
},

 

Client Script:

var getState = g_form.getValue('state');

if(getState == 6)

{

var ga = new GlideAjax('SLADetails'); // SI name
    ga.addParam('sysparm_name', 'checkSLA'); //SI function
    ga.addParam('sysparm_id', newValue);
    ga.getXML(callBack);

    function callBack(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert("answer" + answer);

if(answer == true)

{

g_form.setVisible('sla breached reason'', true);
        g_form.setMandatory('sla breached reason'', true);

 } 

 }

else

{

g_form.setVisible('sla breached reason'', false);
        g_form.setMandatory('sla breached reason'', false);

}

}

Regards
Harish