Need a script to check whether all the Incident Tasks of an Incident are in resolved state, then only the the incident state can move into Resolved state, any BR any script?

scottyharris
Kilo Contributor

Need a script to check whether all the Incident Tasks of an Incident are in resolved state, then only the the incident state can move into Resolved state, any BR any script?

9 REPLIES 9

@jayaprakash1998 

Can you post a new question for this?

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

The Requirement is I need a script to check is incident state is resolved then all the incident task should be marked as closed complete automatically.

Hello @jayaprakash1998 

 

Please refer to my previous post in which I tagged you.

 

Thanks  & Regards

Kartik Magadum

Hello @jayaprakash1998 


To ensure that all Incident Tasks are marked as "Closed Complete" when the Incident itself is marked as "Resolved," you can use a Business Rule (BR)

 

Here's a script example for this Business Rule

(function executeRule(current, previous /*, display_error*/) {
    if (current.state == 6 /* '6' represents the resolved state in this example */) {
        // Query all related Incident Tasks for the current Incident
        var incidentTaskGr = new GlideRecord('incident_task');
        incidentTaskGr.addQuery('incident', current.sys_id); // Replace 'incident' with the actual reference field name
        incidentTaskGr.query();

        // Update all related Incident Tasks to 'Closed Complete'
        while (incidentTaskGr.next()) {
            incidentTaskGr.setValue('state', 7); // '7' represents the 'Closed Complete' state (change this according to your instance)
            incidentTaskGr.update();
        }
    }
})(current, previous);

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum


 

Kartik Magadum
Kilo Sage

Hello @scottyharris 

To ensure that all Incident Tasks of an Incident are in the resolved state before allowing the Incident itself to move into the resolved state, you can use a Business Rule (BR).

 

Here's a script example for such a Business Rule:

(function executeRule(current, previous /*, display_error*/) {
    // Query all related Incident Tasks for the current Incident
    var incidentTaskGr = new GlideRecord('incident_task');
    incidentTaskGr.addQuery('incident', current.sys_id); // Replace 'incident' with the actual reference field name
    incidentTaskGr.query();

    // Check if all related Incident Tasks are resolved
    var allResolved = true;
    while (incidentTaskGr.next()) {
        if (incidentTaskGr.state != 3 /* '3' is the resolved state in the incident_task table */) {
            allResolved = false;
            break;
        }
    }

    // If all Incident Tasks are resolved, allow the Incident to move to the resolved state
    if (allResolved) {
        current.setWorkflow(false); // Disable any workflow that may be triggered
        current.state = 6; // Set '6' as the resolved state (change this according to your instance)
        current.update();
    } else {
        // If not all Incident Tasks are resolved, cancel the update
        gs.addErrorMessage("All Incident Tasks must be resolved before resolving the Incident.");
        current.setAbortAction(true);
    }
})(current, previous);

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum