Problem task must be completed before closing the problem form

NishaB
Tera Expert

Hi

I want a task to be done. I want to close my problem record if only all the problem tasks are closed. Otherwise an error message has to be displayed. How to configure this?

5 ACCEPTED SOLUTIONS

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @NishaB 

https://www.servicenow.com/community/developer-forum/problem-should-not-close-unless-the-problem-tas...

https://www.servicenow.com/community/itsm-forum/closing-of-problem-record-only-when-all-the-problem-...

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

View solution in original post

Vishal Jaswal
Giga Sage

Hello @NishaB 

 

Create a before business rule on problem table:

 

(function executeRule(current, previous /*null when async*/) {
var problemTasks = new GlideRecord('problem_task');
problemTasks.addQuery('problem', current.sys_id);
problemTasks.addQuery('state', '!=', '3'); 
problemTasks.query();

if (problemTasks.hasNext()) {
gs.addErrorMessage("Cannot close the problem record because not all problem tasks are closed.");
current.setAbortAction(true); // Prevents the record from being saved
}
})(current, previous);


Hope that helps!

View solution in original post

Shivalika
Mega Sage

Hello @NishaB 

 

Create a "Before Update" BR - with condition as state "changes to" "closed" and add below 👇 script 

 

// Trigger only if state is changing to 'Closed'

if (current.state.changesTo('Closed')) {

    // Check for any open Problem Tasks

    var taskGR = new GlideRecord('problem_task');

    taskGR.addQuery('problem_id', current.sys_id);

    taskGR.addQuery('state', '!=', 'Closed');

    taskGR.query();

 

    if (taskGR.hasNext()) {

        gs.addErrorMessage('Cannot close the Problem record because one or more Problem Tasks are still open.');

        current.setAbortAction(true

);

    }

}

 

Make changes as per your field names. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@NishaB 

you can use before update business rule on problem table and use GlideAggregate to optimize rather than using getRowCount()

Condition: State Changes to Closed/Resolved

Script:

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

    // Use GlideAggregate to count problem tasks that are not closed
    var agg = new GlideAggregate('problem_task');
    agg.addQuery('problem', current.problem); // Filter by the current problem record
    agg.addEncodedQuery('stateNOT IN3,4,7'); // Count tasks that are not closed
    agg.addAggregate('COUNT'); // Aggregate the count of matching records
    agg.query();
    if (agg.next() && parseInt(agg.getAggregate('COUNT')) > 0) {
        // If there are any open problem tasks, display an error message
        gs.addErrorMessage('Cannot close the problem record because there are open problem tasks.');
        current.setAbortAction(true); // Prevent the problem record from being closed
    }

})(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

View solution in original post

@NishaB 

Hope you are doing good.

Did my reply answer your question?

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@NishaB 

you can use before update business rule on problem table and use GlideAggregate to optimize rather than using getRowCount()

Condition: State Changes to Closed/Resolved

Script:

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

    // Use GlideAggregate to count problem tasks that are not closed
    var agg = new GlideAggregate('problem_task');
    agg.addQuery('problem', current.problem); // Filter by the current problem record
    agg.addEncodedQuery('stateNOT IN3,4,7'); // Count tasks that are not closed
    agg.addAggregate('COUNT'); // Aggregate the count of matching records
    agg.query();
    if (agg.next() && parseInt(agg.getAggregate('COUNT')) > 0) {
        // If there are any open problem tasks, display an error message
        gs.addErrorMessage('Cannot close the problem record because there are open problem tasks.');
        current.setAbortAction(true); // Prevent the problem record from being closed
    }

})(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

@NishaB 

Hope you are doing good.

Did my reply answer your question?

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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