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

Nayeem A
Tera Expert

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);