How to restrict closure of problem record if task open?

jugantanaya
Tera Contributor

Hi,

Requirement is to restrict the closure of the problem record if any task is open.

One Business rule i have created but problem manager and problem admin able to close the problem record.

 

Business Rule:(Update)

When: before

State = Resolved

Script:

(function executeRule(current, previous /*null when async*/) {
var rec = new GlideRecord('problem_task');
rec.addQuery('problem', current.sys_id);
rec.addQuery('active', true);
rec.query();
//If any of the tasks are active abort the submission
if (rec.hasNext()) {
    gs.addInfoMessage('Submission aborted due to active child tasks.');
    current.setAbortAction(true);
}
}) (current, previous);

 

1 ACCEPTED SOLUTION

M Iftikhar
Mega Sage

 

Hi @jugantanaya,

You can add a role-based check directly in your Business Rule like this:

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

    if (gs.hasRole('problem_manager') || gs.hasRole('problem_admin')) {
        var rec = new GlideRecord('problem_task');
        rec.addQuery('problem', current.sys_id);
        rec.addQuery('active', true);
        rec.query();

        if (rec.hasNext()) {
            gs.addErrorMessage('You cannot resolve or close this Problem because there are still active Problem Tasks.');
            current.setAbortAction(true);
        }
    }

})(current, previous);


This ensures Problem Managers and Admins are also prevented from closing if tasks are still active.

 

Thanks & Regards,  
Muhammad Iftikhar 
 
If my response helped, please mark it as the accepted solution so others can benefit as well. 

View solution in original post

6 REPLIES 6

Bhuvan
Kilo Patron

@jugantanaya 

 

Ideal way to manage this via state transition model. As per your Problem Management process requirements, configure the state transition model and this would work without any code customizations.

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB2178948

 

If this helped to answer your query, please mark it helpful & accept the solution. 

 

Thanks,

Bhuvan

Ankur Bawiskar
Tera Patron
Tera Patron

@jugantanaya 

if problem manager and admin are allowed to close then ensure BR doesn't run for them

add this in condition field

!(gs.hasRole('admin') || gs.hasRole('problem_manager'))

Script:

(function executeRule(current, previous /*null when async*/ ) {
    var rec = new GlideRecord('problem_task');
    rec.addQuery('problem', current.sys_id);
    rec.addQuery('active', true);
	rec.setLimit(1);
    rec.query();
    //If any of the tasks are active abort the submission
    if (rec.hasNext()) {
        gs.addInfoMessage('Submission aborted due to active child tasks.');
        current.setAbortAction(true);
    }
})(current, previous);

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