Make a field mandatory on Incident table if SLA is breached

now developer
Tera Contributor

Hello all,

 

I have added a field on incident form to make the field visible and mandatory when SLA is breached.Trying to achieve this with Business rules providing some triggers , and also through onChange or onLoad client script, unable to achieve it. Can someone help me on this?

 

(function executeRule(current, previous /*null when async*/) {
    // Get the related incident record
    var incident = new GlideRecord('incident');
    if (incident.get(current.task)) {
        // Check if the SLA is breached
        if (current.stage == 'breached') {
            // Make the Reason for SLA breach field visible and mandatory
            incident.u_reason_for_sla_breach.setDisplay(true);
            incident.u_reason_for_sla_breach.setMandatory(true);
        } else {
            // Hide the Reason for SLA breach field and make it optional if not breached
            incident.u_reason_for_sla_breach.setDisplay(false);
            incident.u_reason_for_sla_breach.setMandatory(false);
        }
        incident.update();
    }
})(current, previous);
 
Any help would be appreciated!
1 ACCEPTED SOLUTION

Rishi_11
Kilo Sage

Hi @now developer,

I see that you are trying .setDisplay() and .setMandatory() in your BR, unfortunately these don't work on server side.

Here you need to pass data from server to client side. First on server side you'll need to check if any sla associated with INC has breached and then you'll need to pass that data to client script to make the field visible and mandatory. There are a couple of ways you can achieve this; script include and client script or display business rule and client script.

I would recommend using a Display BR and onLoad client script in your case. You can also try script include if you want.

 

Display BR on Incident table:

This script checks related SLAs for the INC and if any of them has breached it populates true in g_scratchpad.has_breached variable.

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

	// Add your code here
	var sla = new GlideRecord("task_sla");
	sla.addQuery('task', current.sys_id);
	sla.addQuery('has_breached', true);
	sla.query();
	if(sla.next()){
		g_scratchpad.has_breached = true;
	}

})(current, previous);

 

OnLoad Client Script:  Here we check the scratchpad variable and make our field visible and mandatory.

function onLoad() {
   //Type appropriate comment here, and begin script below
 if (g_scratchpad.has_breached == true){
g_form.setVisible('u_reason_for_sla_breach', true);
g_form.setMandatory('u_reason_for_sla_breach', true);
   }
}

 

I am assuming this custom field is not visible by default, so I am just making it visible and mandatory. Feel free to modify according to your requirements.

 

Please mark this response helpful or correct, if it helped you.

Regards,

Rishi.

View solution in original post

13 REPLIES 13

Hello Rishi,

 

There are no UI Policies w.r.t this field. Please find the attached scripts

 

Thanks

 

Thanks Rishi, It worked. Actually I was trying to write this BR on task SLA table.

 

Thank you once again for your prompt responses,

Dhruv Chandan
Giga Guru

Hi,

 

This can be achieved with just the out of the box fields. An on load client script, which will call a script include to validate if the incident SLA has breached or not.

 

OnLoad client script:

 

 

 

function onLoad() {

    function validateSLABreach() {

        var gaSLA = new GlideAjax('global.validateIncidentSLA');
        gaSLA.addParam('sysparm_name', 'validateBreach');
        gaSLA.addParam('sysparm_incident_sysID', g_form.getValue("sys_id"));
        gaSLA.getXML(hasBreachVal);



        function hasBreachVal(response) {

            var answer = response.responseXML.documentElement.getAttribute("answer");

            if (answer == true) {
                // Visible and Mandatory
                g_form.setDisplay('u_reason_for_sla_breach', true);
                g_form.setMandatory('u_reason_for_sla_breach', true);
            } else {
                // Hide and non mandatory
                g_form.setDisplay('u_reason_for_sla_breach', false);
                g_form.setMandatory('u_reason_for_sla_breach', false);
            }

        }
    }
}

// Function called to execute script
validateSLABreach();

}

 

 
Script Include - Client callable:

 

 

var validateIncidentSLA = Class.create();
validateIncidentSLA.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateBreach: function() {

        var incident_sysID = this.getParameter('sysparm_incident_sysID');
        var grSLAVal = new GlideRecord("task_sla");
        grSLAVal.addEncodedQuery("task=" + incident_sysID)
        grSLAVal.query();
        while (grSLAVal.next()) {
            if (grSLAVal.has_breached == true) {
                return true;
            }
            break;
        }

    },

    type: 'validateIncidentSLA'
});

 

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Dhruv Chandan
 

 
 

Kris Moncada
Tera Guru
  1. Create a client callable Script Include:

 

var IncidentGlideAjaxTest = Class.create();
IncidentGlideAjaxTest.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getBreachStatus: function(){

		var incident_sys_id = this.getParameter('sysparm_incident_sys_id');

		var isBreached = false;

		//task=1c741bd70b2322007518478d83673af3^stage=breached
		//task_sla_list
		var gr_task_sla = new GlideRecord('task_sla');
		gr_task_sla.addQuery('task', incident_sys_id);
		gr_task_sla.addQuery('stage', 'breached');
		gr_task_sla.query();

		if(gr_task_sla.hasNext()){
			isBreached = true;
		}



		return isBreached;
	},


    type: 'IncidentGlideAjaxTest'
});​

 

  • Create an onload client script on the incident table.

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    console.log('incident sys_id = ' + g_form.getUniqueValue());
    var ga = new GlideAjax('IncidentGlideAjaxTest');
    ga.addParam('sysparm_name', 'getBreachStatus');
    ga.addParam('sysparm_incident_sys_id', g_form.getUniqueValue());
    ga.getXMLAnswer(handleResponse);
}

function handleResponse(response){
	console.log(response);
	if(response == 'true'){
		g_form.addInfoMessage('This incident has a breach SLA.');
		g_form.setMandatory('description', true);
	}
}​

 

In this sample, I made the description field mandatory if the incident has a breached SLA.