- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
"When an incident is resolved, if it is linked to a problem, the system should check if all other
related incidents are also resolved and then automatically resolve the problem after saving."
This is a scenario, How can i do this scenario give me the right code to implement it.
Note: Don't tell me the AI-related code.
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Create a after update Business rule
Condition : State changes to Resolved
Script :
if (current.problem_id.nil()) {
return;
}
var p = new GlideRecord('problem');
if (!p.get(current.problem_id)) {
return;
}
var i = new GlideRecord('incident');
i.addQuery('problem_id', current.problem_id);
i.addQuery('state', '!=', 6);
i.addQuery('sys_id', '!=', current.sys_id);
i.query();
if (!i.hasNext()) {
p.state = 6;
p.close_code = 'Solved (Permanently)';
p.close_notes = 'Auto-resolved as all related incidents are resolved';
p.update();
}
Do let me know if you need any explanation of the code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
ideally it happens reverse
When PRB is closed then related INCs associated to this PRB are closed
This happens with OOTB BR "SNC - ITIL - Close Related"
Close related Incidents upon closing a Problem
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is a classic use case for an After Business Rule on the Incident table. Here is how to implement it:
Business Rule Setup:
- Table: Incident
- When: After
- Operation: Update
- Condition: current.state == 6 && current.problem_id != null (state 6 = Resolved)
Script:
(function executeRule(current, previous) {
var problemId = current.problem_id;
// Check if all incidents linked to this problem are resolved
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('problem_id', problemId);
incidentGr.addQuery('state', '!=', 6); // not resolved
incidentGr.addQuery('sys_id', '!=', current.sys_id); // exclude current
incidentGr.query();
// If no unresolved incidents found, resolve the problem
if (!incidentGr.hasNext()) {
var problemGr = new GlideRecord('problem');
if (problemGr.get(problemId)) {
problemGr.state = 4; // 4 = Resolved in Problem table
problemGr.resolution_code = 'fix_applied';
problemGr.resolution_notes = 'All related incidents have been resolved.';
problemGr.update();
}
}
})(current, previous);A few things to keep in mind:
- Problem state value 4 is the default for Resolved but verify this in your instance as it can vary depending on customizations
- The resolution_code field may also have different values in your instance, check the Problem form for available options
Hope this helps!
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Create a after update Business rule
Condition : State changes to Resolved
Script :
if (current.problem_id.nil()) {
return;
}
var p = new GlideRecord('problem');
if (!p.get(current.problem_id)) {
return;
}
var i = new GlideRecord('incident');
i.addQuery('problem_id', current.problem_id);
i.addQuery('state', '!=', 6);
i.addQuery('sys_id', '!=', current.sys_id);
i.query();
if (!i.hasNext()) {
p.state = 6;
p.close_code = 'Solved (Permanently)';
p.close_notes = 'Auto-resolved as all related incidents are resolved';
p.update();
}
Do let me know if you need any explanation of the code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
ideally it happens reverse
When PRB is closed then related INCs associated to this PRB are closed
This happens with OOTB BR "SNC - ITIL - Close Related"
Close related Incidents upon closing a Problem
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is a classic use case for an After Business Rule on the Incident table. Here is how to implement it:
Business Rule Setup:
- Table: Incident
- When: After
- Operation: Update
- Condition: current.state == 6 && current.problem_id != null (state 6 = Resolved)
Script:
(function executeRule(current, previous) {
var problemId = current.problem_id;
// Check if all incidents linked to this problem are resolved
var incidentGr = new GlideRecord('incident');
incidentGr.addQuery('problem_id', problemId);
incidentGr.addQuery('state', '!=', 6); // not resolved
incidentGr.addQuery('sys_id', '!=', current.sys_id); // exclude current
incidentGr.query();
// If no unresolved incidents found, resolve the problem
if (!incidentGr.hasNext()) {
var problemGr = new GlideRecord('problem');
if (problemGr.get(problemId)) {
problemGr.state = 4; // 4 = Resolved in Problem table
problemGr.resolution_code = 'fix_applied';
problemGr.resolution_notes = 'All related incidents have been resolved.';
problemGr.update();
}
}
})(current, previous);A few things to keep in mind:
- Problem state value 4 is the default for Resolved but verify this in your instance as it can vary depending on customizations
- The resolution_code field may also have different values in your instance, check the Problem form for available options
Hope this helps!
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello Ahsan,
Thank you for your support.
