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

Community Alums
Not applicable

Hi @now developer ,

 

Via BR its not possible bcoz BRs are server side scripts and wont manipulate the client-side behavior of form fields such as visibility or mandatory status without a page reload,

You should use Client Scripts to achieve this, which run on the client side and can interact with the form as it's being used.

1. onLoad Client Script

This script will check the status of the SLA when the incident form is loaded and adjust the field properties accordingly.

function onLoad() {
    // Function to check SLA status and adjust field properties
    function adjustFieldProperties() {
        var slaStatus = g_form.getValue('stage'); // Assuming 'stage' holds the SLA status
        if (slaStatus === 'breached') {
            g_form.setDisplay('u_reason_for_sla_breach', true); // Make field visible
            g_form.setMandatory('u_reason_for_sla_breach', true); // Make field mandatory
        } else {
            g_form.setDisplay('u_reason_for_sla_breach', false); // Hide the field
            g_form.setMandatory('u_reason_for_sla_breach', false); // Field is not mandatory
        }
    }

    // Call the function to adjust fields as per SLA status
    adjustFieldProperties();
}

 

2. onChange Client Script

This script will be triggered when the SLA status changes on the form, enabling real-time updates without reloading the form.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    if (control == 'stage') { // Assuming 'stage' is the field that reflects SLA status
        if (newValue === 'breached') {
            g_form.setDisplay('u_reason_for_sla_breach', true); // Make field visible
            g_form.setMandatory('u_reason_for_sla_breach', true); // Make field mandatory
        } else {
            g_form.setDisplay('u_reason_for_sla_breach', false); // Hide the field
            g_form.setMandatory('u_reason_for_sla_breach', false); // Field is not mandatory
        }
    }
}

 

This client-side scripting approach ensures that the field visibility and mandatory status are dynamically updated on the incident form based on the SLA status.

 

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

 

Thanks & Regards,

Sanjay Kumar

Thank you for prompt response. But onChange which field I should give? I tried giving State field changes but didn't work. I am still trying to fix, but not working.

 

Thanks,

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.

No luck! 

 

I am writing the BR on incident table and onLoad also on incident table. But still it is not mandatory even though I have tickets breached and when trying to close, its not mandate.