Write a bussiness rule when incident is closed then associated problem will be closed.

S Kuanar
Tera Contributor
(function executeRule(current, previous /*null when async*/) {

    var inc = new GlideRecord('problem');
    inc.addQuery('incident',current.sys_id);
    inc.addQuery('state','!=',107);
    inc.query();
    while(inc.next()){
        inc.state=107;
        //inc.close_notes='close';
        inc.update();
    }

})(current, previous);
 
 
Why this above code didn't executed ?
7 REPLIES 7

@S Kuanar 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Tai Vu
Kilo Patron
Kilo Patron

Hi @S Kuanar 

There seems to be a misunderstanding in your requirement. Typically, multiple Incidents are linked to a Problem, and the Problem remains open until the Root Cause Analysis (RCA) is completed and the issue is resolved. It wouldn’t make sense to automatically close a Problem just because an Incident is resolved.

 

Additionally, your script won't work due to this line:

 

inc.addQuery('incident', current.sys_id);

 

The Problem [problem] table does not have the incident field. The relationship works the other way around, each Incident can reference a Problem through the problem_id field. => 1 problem and multiple incidents

 

Cheers,
Tai Vu

Vishal Jaswal
Giga Sage

Hello @S Kuanar 


vishal_jaswal_0-1741324814741.png

 

Four Mandatory fields values of problem table/form were missing, hence modified your code:

 

 

var inc = new GlideRecord('problem');
inc.addQuery('incident', current.sys_id); //considering you have incident column name/field in the problem table/form else ('sys_id', current.problem_id) will work too.
inc.addQuery('state', '!=', 107);
inc.query();
while (inc.next()) {
    inc.cause_notes = 'Test1';
    inc.close_notes = 'Test2'; // can be equivalent to current.close_notes
    inc.fix_notes = 'Test3';
    inc.resolution_code = 'fix_applied'; // only 4 choices are available oob
    inc.state = 107;
    //inc.close_notes='close';
    inc.update();
}

 

 

Now, if above is for learning purpose then it is okay. You might have also learnt that missing fields added above are really relevant to capture detailed information before closing the problem.

So, if you are given such requirement then have a brainstorming session with your peers as well as your stakeholders that problem are open to identify the underlying root cause, capture the costs associated with this problem and a solution to avoid such in future.


Hope that helps!


Hope that helps!