Resolve incident only with attachment if SLA is breached

Sidharth Jain
Tera Contributor

Hi team, I have a scenario that if any SLA is breached then, user should only be able to resolve incident after attaching an attachment and If there is no attachment and one or more SLAs is breached then user should not be able to resolve the incident. Please help me in scripting this. Thanks in advance!

1 ACCEPTED SOLUTION

amitshishod
Tera Guru

Hello @Sidharth Jain ,

You need to use Before Update Business rule and filter condition will be "State changes to resolved".
You can try using below script.

var count = new GlideAggregate('sys_attachment');

    count.addAggregate('COUNT');

    count.addEncodedQuery("table_sys_id=" + current.sys_id);

    count.query();

    var attach_count = 0;

    if (count.next())

        attach_count = count.getAggregate('COUNT');

 

 

    var gr= new GlideRecord("task_sla");

    gr.addEncodedQuery("task=" + current.sys_id); 

    gr.query();

    while (gr.next()) {

        if (gr.has_breached && attach_count == 0) {

            current.setAbortAction(true);

            break;

        }

    }



Please mark my answer correct and helpful if it helps you.

View solution in original post

4 REPLIES 4

amitshishod
Tera Guru

Hello @Sidharth Jain ,

You need to use Before Update Business rule and filter condition will be "State changes to resolved".
You can try using below script.

var count = new GlideAggregate('sys_attachment');

    count.addAggregate('COUNT');

    count.addEncodedQuery("table_sys_id=" + current.sys_id);

    count.query();

    var attach_count = 0;

    if (count.next())

        attach_count = count.getAggregate('COUNT');

 

 

    var gr= new GlideRecord("task_sla");

    gr.addEncodedQuery("task=" + current.sys_id); 

    gr.query();

    while (gr.next()) {

        if (gr.has_breached && attach_count == 0) {

            current.setAbortAction(true);

            break;

        }

    }



Please mark my answer correct and helpful if it helps you.

Ankur Bawiskar
Tera Patron
Tera Patron

@Sidharth Jain 

you can use before update BR when state changes to Resolved

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

    // Check if there are any breached SLAs
    var slaGr = new GlideRecord('task_sla');
    slaGr.addQuery('task', current.sys_id);
    slaGr.addEncodedQuery('has_breached=true');
    slaGr.query();
    if (slaGr.hasNext()) {
        // Check if there are any attachments
        var attachmentGr = new GlideRecord('sys_attachment');
        attachmentGr.addQuery('table_sys_id', current.sys_id);
        attachmentGr.query();
        if (!attachmentGr.hasNext()) {
            gs.addErrorMessage('You must attach a file before resolving this incident due to breached SLA(s).');
            current.setAbortAction(true);
        }
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Sidharth Jain 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

Rajesh Chopade1
Mega Sage

Hi @Sidharth Jain 

 

You can have bellow approach to resolve your issue:

 

 

  • Check for Breached SLAs: You will need to check if there are any breached SLAs related to the incident.
  • Check for Attachments: You need to ensure that if any SLA is breached, the user cannot resolve the incident unless they attach at least one file.
  • Prevent Resolution: If there are breached SLAs and no attachment, you’ll prevent the resolution of the incident.

 

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

    // Check if the incident state is being changed to "Resolved"
    if (current.state == 6) {  // Assuming 6 is the state ID for 'Resolved'

        // Query the SLAs related to this incident
        var slaGr = new GlideRecord('sla_task');
        slaGr.addQuery('task', current.sys_id);  // Reference to the Incident
        slaGr.addQuery('sla.breached', true);  // Check if the SLA is breached
        slaGr.query();

        // If there are any breached SLAs, check if there is an attachment
        if (slaGr.hasNext()) {
            var attachmentGr = new GlideRecord('sys_attachment');
            attachmentGr.addQuery('table_name', 'incident');
            attachmentGr.addQuery('table_record', current.sys_id);
            attachmentGr.query();

            // If no attachments found, prevent the resolution
            if (!attachmentGr.hasNext()) {
                current.setAbortAction(true);  // Prevent the resolution
                gs.addErrorMessage('Please attach a file before resolving the incident due to SLA breach.');
            }
        }
    }

})(current, previous);

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh