Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Query Related to Business Rule

prakharbt20
Tera Contributor

"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.

3 ACCEPTED SOLUTIONS

TharaS657398130
Giga Guru

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

View solution in original post

Ankur Bawiskar
Tera Patron

@prakharbt20 

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! 🙏

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

View solution in original post

AhsanM
Tera Expert

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!

Ahsan
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts

View solution in original post

4 REPLIES 4

TharaS657398130
Giga Guru

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

Ankur Bawiskar
Tera Patron

@prakharbt20 

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! 🙏

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

AhsanM
Tera Expert

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!

Ahsan
ServiceNow Developer & Admin
Builder of NowFixer | Free AI debugging tool for ServiceNow scripts

Hello Ahsan,

 

Thank you for your support.