AutoClose Related incidents when incident state is onhold, when problem state is resolved

Sriram17
Tera Contributor

Hi Team,

 

I need to Resolve Related incidents when incident state is onhold, when problem state is resolved.

 

I have written after insert or update business rules. Condition is state is resolved.

 

Script:

var incidentGr = new GlideRecord('incident');
        incidentGr.addQuery('problem_id', current.sys_id);
        incidentGr.addQuery('state', 3);
        incidentGr.query();

        // Loop through related incidents and close them
        while (incidentGr.next()) {
            incidentGr.state = 6;
            incidentGr.close_notes = "Automatically Resolved due to resolution of associated problem: " + current.number;
            incidentGr.update();
        }

 

Sriram17_0-1715191371058.png

 

Sriram17_1-1715191538107.png

 

@Voona Rohila 

2 REPLIES 2

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Sriram17 

Can you explain in detail of what is not working?

Change the condition to " State changes to Resolved".( So that It won't trigger if any more updates are done in resolved state)


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

swathisarang98
Giga Sage
Giga Sage

Hi @Sriram17 ,

 

You need to add one more line of code as below,

incidentGr.close_code ='Resolved by problem'; // as this is mandatory for incident to close or resolving, change the value according to your need and change the condition to state changes to Resolved .
 

 

 

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

    var incidentGr = new GlideRecord('incident');
    incidentGr.addQuery('problem_id', current.sys_id);
    incidentGr.addQuery('state', 3);
    incidentGr.query();

    
    while (incidentGr.next()) {
		gs.info('inside while line no 10');
        incidentGr.state = 6;
		incidentGr.close_code ='Resolved by problem';
        incidentGr.close_notes = "Automatically Resolved due to resolution of associated problem: " + current.number;
        incidentGr.update();
    }

})(current, previous);

 

 

 Tried same in PDI its working please do give it a try,
 

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

Regards,

Swathi Sarang