prevent problem to be close/cancel if any problem task are open

Prakash_S
Tera Contributor
 
2 ACCEPTED SOLUTIONS

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can create before BR and use below script.

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

    var taskGr = new GlideRecord('problem_task');
    taskGr.addQuery('problem', current.sys_id); 
    taskGr.addQuery('state', '!=', '157'); 
    taskGr.query();


    if (taskGr.next()) {
        gs.addErrorMessage('The problem cannot be closed or canceled because there are open problem tasks.');
        current.setAbortAction(true); 
    }
})(current, previous);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

yuvarajkate
Giga Guru

Hey Prakash, you can write a before[update] business rule for this.

(function executeRule(current, previous /*null when async*/) {
    // Check if the Problem state is transitioning to Closed or Canceled
    var closingStates = ['7', '8']; 
    if (closingStates.includes(current.state.toString())) {
        var taskGR = new GlideRecord('problem_task');
        taskGR.addQuery('problem', current.sys_id);
        taskGR.addQuery('state', '!=', '3'); // Replace with the value for the 'Closed' state
        taskGR.query();

        if (taskGR.hasNext()) {
            gs.addErrorMessage('The Problem cannot be closed or canceled while there are open Problem Tasks.');
            current.setAbortAction(true);
        }
    }
})(current, previous);

 

Please do mark it as Helpful/Correct if it helped you in any way.

View solution in original post

4 REPLIES 4

Runjay Patel
Giga Sage

Hi @Prakash_S ,

 

You can create before BR and use below script.

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

    var taskGr = new GlideRecord('problem_task');
    taskGr.addQuery('problem', current.sys_id); 
    taskGr.addQuery('state', '!=', '157'); 
    taskGr.query();


    if (taskGr.next()) {
        gs.addErrorMessage('The problem cannot be closed or canceled because there are open problem tasks.');
        current.setAbortAction(true); 
    }
})(current, previous);

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

Najmuddin Mohd
Mega Sage

Hi @Prakash_S ,

You can write a before Business rule, when state changes to Close / Cancel.
GlideRecord the problem task table and query the parent with current problem and check whether are there any problem task in Open state.

If it has any records, abort the action.

If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.

yuvarajkate
Giga Guru

Hey Prakash, you can write a before[update] business rule for this.

(function executeRule(current, previous /*null when async*/) {
    // Check if the Problem state is transitioning to Closed or Canceled
    var closingStates = ['7', '8']; 
    if (closingStates.includes(current.state.toString())) {
        var taskGR = new GlideRecord('problem_task');
        taskGR.addQuery('problem', current.sys_id);
        taskGR.addQuery('state', '!=', '3'); // Replace with the value for the 'Closed' state
        taskGR.query();

        if (taskGR.hasNext()) {
            gs.addErrorMessage('The Problem cannot be closed or canceled while there are open Problem Tasks.');
            current.setAbortAction(true);
        }
    }
})(current, previous);

 

Please do mark it as Helpful/Correct if it helped you in any way.

vishwajeet5550
Mega Guru
current.state == 'Closed' || current.state == 'Canceled'

var openTasks = new GlideRecord('problem_task');
openTasks.addQuery('problem', current.sys_id);
openTasks.addQuery('state', 'IN', '1,2'); // Open or In Progress tasks
openTasks.query();

if (openTasks.hasNext()) {
    gs.addErrorMessage('Cannot close this problem because there are open problem tasks.');
    action.setAbortAction(true); // Prevent the UI action from completing
}

var openTasks = new GlideRecord('problem_task');
openTasks.addQuery('problem', current.sys_id); // Find tasks related to the current Problem
openTasks.addQuery('state', 'IN', '1,2'); // Check for open states (typically 1 = Open, 2 = In Progress)
openTasks.query();

if (openTasks.hasNext()) {
    // If open Problem Tasks exist, prevent the state change to Closed or Canceled
    gs.addErrorMessage('Cannot close or cancel this problem because there are open problem tasks.');
    current.setAbortAction(true); // Prevent record save
}