The CreatorCon Call for Content is officially open! Get started here.

Close the problem once all related incidents have been Closed.

SparshG
Tera Contributor

I was working for a requirement which states: Create a business rule to automatically close the problem once all related incidents have been Resolved. In order to achieve it I've written one BR( after update, with condition state changes to Closed). So please help me to find an optimised solution for this one.

 

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

 

    // Add your code here

    // Check if the incident state is closed

    if (current.state.changes() && current.state == '7') {

        var problemGR = new GlideRecord('problem');

        problemGR.get(current.problem_id);

       

        var incidentGR = new GlideRecord('incident');

        incidentGR.addQuery('problem_id', problemGR.sys_id);

        incidentGR.addQuery('state', '!=', '7'); // Exclude closed incidents

        incidentGR.query();

 

        // If there are no incidents left in any state other than closed

        if (!incidentGR.hasNext()) {

            // Close the problem

            if(problemGR.state == '101'){ // state is new; as we can't close a problem at 'New' state 

                problemGR.assigned_to="73ab3f173b331300ad3cc9bb34efc4df"; // sys_id of any user

                problemGR.state="102";

                problemGR.update();

            }

            problemGR.resolution_code = "Resolution resolved";

            problemGR.state = '107'; // Set to closed

            problemGR.update();

        }

    }

 

})(current, previous);

 

4 REPLIES 4

ELMAHDI
Tera Expert

 

Hello @SparshG,

Yes, you can optimize this code but, you can implement the requirement to automatically close the problem once all related incidents are resolved without scripting by utilizing ServiceNow's out-of-the-box features like workflow or Flow Designer.  Which more easy and at the same time you are using best practice configuration instead of customization. 

 

If you find this comment helpful, don't forget to click on the Helpful Button 😊

Uncle Rob
Kilo Patron

PROCESS NOTES

THIS SOUNDS LIKE A VERY BAD IDEA.  The point of Problem isn't to wait until all incidents are finished.  The point of Problem is to investigate non-known malfunctions.  You close Problem when you ...
- FIX it
- Develop a workaround
- Decide to live with the issue.

Closing Incident just means service is restored.  If you're closing problems once the related incidents are complete, you're totally undermining the intent of Problem.

TECHNICAL NOTES
  
I wouldn't do this via script in a business rule.  I'd do it via Flow Designer.
MUCH simpler and intuitive to build.  MUCH easier for those who come after you to find and understand.
This video isn't the precise use case (it closes Incidents after Incident Tasks are done), but you can easily modify it to your needs.






Hi @SparshG ,

 

i fully agree with @Uncle Rob - the intended is not to have problem closed when all incident is closed, but to close incidents when service is restored by work around, and then have a problem to investigate the underlaying root cause.

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

 Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

ELMAHDI
Tera Expert

 

Hello @SparshG,

Yes, you can optimize this code but, you can implement the requirement to automatically close the problem once all related incidents are resolved without scripting by utilizing ServiceNow's out-of-the-box features like workflow or Flow Designer.  Which more easy and at the same time you are using best practice configuration instead of customization. 

 

Here's an example : 

Capture d’écran 2024-10-13 à 13.45.26.png

 If you find this comment helpful, don't forget to click on the Helpful Button 😊