Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set Mandatory field visible on Incident Close

Sherridan Maitl
Kilo Expert

Good Day, I need help with a client script.

I created a field called 'SLA Breached Reason' to display and be mandatory when an Incident State is changed to close and Has Breached is true. I can't get the Task_SLA field through incidents through UI Policy so I need a script. Any ideas? Regards

1 ACCEPTED SOLUTION

swathisarang98
Giga Sage

Hi @Sherridan Maitl ,

 

You can On change Create a client script and script include as shown below,

 

Script Include: client callable 

swathisarang98_0-1708727029376.png

 

 

 

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

    getSlaDetails: function() {
        var taskSlaSysId = this.getParameter('sysparm_sysid');

        var taskSla = new GlideRecord('task_sla');
        taskSla.addQuery('task', taskSlaSysId.toString());
        taskSla.query();
        if (taskSla.next()) {
            gs.info('inside if' + taskSlaSysId);
            if (taskSla.has_breached == true) {
               
                return true;

            } else if (taskSla.has_breached == false || taskSla.has_breached == "false") {
               
                return false;
            }
        }
    },

    type: 'checksla'
});

 

 

 

 

Client script:

swathisarang98_1-1708718613144.png

 

 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {

        // g_form.setMandatory('u_sla_breached_reason', false);
        return;

    }
    g_form.setVisible('u_sla_breached_reason', false); // everywhere replace u_sla_breached_reason with "SLA Breached Reason" backend value

    if (newValue == '7') {
        var sysid = g_form.getUniqueValue();
        var slaTask = new GlideAjax('checksla');
        slaTask.addParam('sysparm_name', 'getSlaDetails');
        slaTask.addParam('sysparm_sysid', sysid);
        slaTask.getXML(getresponse);

    } else {
        
        g_form.setMandatory('u_sla_breached_reason', false);
		g_form.setVisible('u_sla_breached_reason', false);
    }

    function getresponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');

        if (answer == true || answer == 'true') {
            g_form.setVisible('u_sla_breached_reason', true);
            g_form.setMandatory('u_sla_breached_reason', true);

        }
    }
    //Type appropriate comment here, and begin script below

}

 

 

 

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

 

 

View solution in original post

3 REPLIES 3

pat222
Tera Expert

Hi, 

 

you could create a onChange Client Script that runs when State is changed to "closed" and have a GlideAjax call to a script include to check if task_sla is true.

 

Here is an example: 

Script Include:

 

var CheckSLABreached = Class.create();  
CheckSLABreached.prototype = Object.extendsObject(AbstractAjaxProcessor, {  
    hasBreachedSLA: function() {  
        var taskSysId = this.getParameter('sysparm_task_sys_id');  
        var taskSLAgr = new GlideRecord('task_sla');  
        taskSLAgr.addQuery('task', taskSysId);  
        taskSLAgr.query();  
        while (taskSLAgr.next()) {  
            if (taskSLAgr.has_breached == 'true') {  
                return true;  
            }  
        }  
        return false;  
    },  
  
    type: 'CheckSLABreached'  
});  

 Client Script: 

function onChange(control, oldValue, newValue, isLoading) {  
    if (isLoading || newValue === '') {  
        return;  
    }  
  
    var stateClosedValue = '7'; // Replace with the actual value for 'Closed' in your instance  
  
    if (newValue == stateClosedValue) {  
        // Make the GlideAjax call to check for SLA breach  
        var ga = new GlideAjax('CheckSLABreached');  
        ga.addParam('sysparm_name', 'hasBreachedSLA');  
        ga.addParam('sysparm_task_sys_id', g_form.getUniqueValue());  
        ga.getXMLAnswer(checkForSLABreach);  
    }  
}  
  
function checkForSLABreach(answer) {  
    g_form.setMandatory('sla_breached_reason', answer);
}  

 I hope this could help. 

swathisarang98
Giga Sage

Hi @Sherridan Maitl ,

 

You can On change Create a client script and script include as shown below,

 

Script Include: client callable 

swathisarang98_0-1708727029376.png

 

 

 

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

    getSlaDetails: function() {
        var taskSlaSysId = this.getParameter('sysparm_sysid');

        var taskSla = new GlideRecord('task_sla');
        taskSla.addQuery('task', taskSlaSysId.toString());
        taskSla.query();
        if (taskSla.next()) {
            gs.info('inside if' + taskSlaSysId);
            if (taskSla.has_breached == true) {
               
                return true;

            } else if (taskSla.has_breached == false || taskSla.has_breached == "false") {
               
                return false;
            }
        }
    },

    type: 'checksla'
});

 

 

 

 

Client script:

swathisarang98_1-1708718613144.png

 

 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {

        // g_form.setMandatory('u_sla_breached_reason', false);
        return;

    }
    g_form.setVisible('u_sla_breached_reason', false); // everywhere replace u_sla_breached_reason with "SLA Breached Reason" backend value

    if (newValue == '7') {
        var sysid = g_form.getUniqueValue();
        var slaTask = new GlideAjax('checksla');
        slaTask.addParam('sysparm_name', 'getSlaDetails');
        slaTask.addParam('sysparm_sysid', sysid);
        slaTask.getXML(getresponse);

    } else {
        
        g_form.setMandatory('u_sla_breached_reason', false);
		g_form.setVisible('u_sla_breached_reason', false);
    }

    function getresponse(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');

        if (answer == true || answer == 'true') {
            g_form.setVisible('u_sla_breached_reason', true);
            g_form.setMandatory('u_sla_breached_reason', true);

        }
    }
    //Type appropriate comment here, and begin script below

}

 

 

 

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

 

 

 

Sherridan Maitl
Kilo Expert

Thanks! This is a great I will update after I'm finished testing