Prevent problem record from being closed if there are pending problem tasks on that record

abdnayeemsh
Tera Contributor

I want to prevent a problem record from closing before its related problem tasks gets closed using the below

(before update) business rule on problem table.

But it is just showing an invalid update pop up and  moving to closed state while there are still pending problem tasks. 

 

 

(function executeRule(current, previous ) {

    if (current.state.changesTo('Closed')) {
        var taskGR = new GlideRecord('problem_task'); 
        taskGR.addQuery('problem', current.sys_id);  
        taskGR.addActiveQuery();
        taskGR.query();

        if (taskGR.next()) {
            gs.addErrorMessage('Cannot close Problem. All associated Problem Tasks must be completed.');
            current.setAbortAction(true); 
        }
    }

 

})(current, previous);

 

 

 

1 REPLY 1

MuddanKT
Mega Contributor

Hi abdnayeemsh, your logic is correct however the possible cause are :

 

1. Using </.changesTo('Closed')/>  instead of checking the numeric state value

State fields store numbers, not labels.
So  </ .changesTo('Closed')/>  may not always behave correctly depending on your dictionary setup.

 

2.  </ setAbortAction(true) /> alone is not enough

On before update, ServiceNow may still proceed with the update unless you also force state rollback.

 

3. Problem Tasks may not use active=true as the right condition

A Problem Task can be </ "Pending", "Work in progress", etc., /> but still have active=true, or sometimes not.

So the most reliable way is to check for states that are NOT closed or canceled.

 

Let me know how this code fix has helped.