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

Hey @now developer ,

 

Can you please share screenshots of the BR and Client Script? So I can help further. The above shared script was working fine on my PDI.

 

Thanks,

Rishi.

Hi @now developer 

I suspect the issue could be that this field is not added to the form. Can you please confirm that you have added 'u_reason_for_sla_breach' on the form? If not please add it to the form using form designer or form layout. Later please utilize the onLoad client script below along with the previously mentioned Display BR

 

Client Script: This will make 'u_reason_for_sla_breach' visible and mandatory if any related SLA has breached otherwise it will hide it.

 

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

 

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

Regards,

Rishi.

Hello Rishi,

 

Please find the screenshots below:

b__DeepthiR_0-1716476170211.pngb__DeepthiR_1-1716476178606.png

 

b__DeepthiR_2-1716476193308.png

b__DeepthiR_3-1716476220589.png

b__DeepthiR_4-1716476275243.png

 

I provided all the screenshots, please let me know if I am going wrong somewhere.

 

Thanks

 

 

Custom field is visible always on the form. Can we write a UI policy to hide it initially and then the scripts run?

I don't think that will work here because Client Scripts runs before UI policy. So, whatever we do in UI policy (which is going to run later) will only reflect on the form.

 

If the field is on the form the above-mentioned BR and Client script should work. Or there might be a UI policy which might be affecting this, can you please check that once? You can check this by going to sys_ui_policy_action.LIST and apply filters like table=incident and field= u_reason_for_sla_breach.

 

If none of these works, please share the BR and client script you have written, so I can help further.

 

Regards,

Rishi.