Business Rule: all Incident Tasks need to be closed before this one

nick135
Tera Expert

Hey Guru's,

 

I've written a Before update Business Rule that should run before my "VP Review" Incident task is closed. The Script I have written checks for any other incident tasks that are open, and have the same parent "Incident" record. If there are any open, then prevent the update from taking place. Below is a image of the code I have written so far. The problem I am having is that it does not seem to be making it to the If statement and never cancels the submission. I've confirmed the line with "incidentTask.addQuery('incident', current.incident.number);" is returning the incident I want, however it does not seem to resolve the query. any help with this is much appreciated!

 

Script:

nick135_0-1687384828668.png

 

1 ACCEPTED SOLUTION

nick135
Tera Expert

I was able to fix the code, below is the finished working product.

 

(function executeRule(current, previous /*null when async*/ ) {
 
    // Check if the incident task task type is 'VP Review'
    if (current.u_major_incident_task_type == '4') {
        //querry the Incident task table for other open tasks related to the same incident.
        var incidentTask = new GlideRecord('incident_task');
        var task = current.number;
var inc = current.incident;
 
        incidentTask.addQuery('incident', inc);
        incidentTask.addQuery('number', '!=', task);
        incidentTask.addEncodedQuery('stateNOT IN3,4,7');
        incidentTask.query();
      
        //if other open tasks are found, prevent closure of the current task
        if (incidentTask.hasNext()) {
            current.setAbortAction(true);
            gs.addErrorMessage('All RCA tasks must be closed before closing the VP Review task.');
        }
    }
 
 
})(current, previous);

View solution in original post

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, this appears to be the same question\issue that you raised here
Prevent Submission of Incident Task when Incident ... - ServiceNow Community

There is no need to create multiple threads for the 1 question\issue and multiple threads\questions are less likely to result in a clear outcome.

 

If you want code reviewed then you should provide the code as plain text, as it is not possible to properly (or quickly) evaluate a screenshot. Clear details of your configuration (your screen-shot does not indicate when\how the BR is triggered), your debugging and diagnostics would also help the community understand your issue; as it's broken\doesn't work provides no useful information.

 

Reviewing your comments and screenshot,
I would start diagnostics by adding logging at the beginning of your BR to ensure that it runs, or confirm this by enabling BR debugging.
Then, log u_major_incident_task_type value and typeof, before your if statement in order to ensure that the value and data type matches your conditions expectations.

 

Looking at your query

addQuery('incident', current.incident.number);
will not work as the platform is expecting the sys_id of the incident, and to reference 'number' this would need to be
addQuery('incident.number', current.incident.number);

 

The encodedQuery doesn't need to check multiple state conditions as you can simply use 'active=true^'
I also think you also need to exclude the current record in the query, otherwise you will always get a result even if there are no other active incident_tasks for the incident.

As a solution I would replace the multiple queries with 1 encoded query, something like

var myQuery = 'active=true^incident=' + current.incident.sys_id + '^sys_id!=' + current.sys_id;

var incidentTask = new GlideRecord('incident_task');
incidentTask.addEncodedQuery(myQuery);
incidentTask.query();

if(incidentTask.hasNext()) {
    gs.info('I have a result');
} else {
    gs.info('No matching Incident tasks');
}

 

so I've logged plenty of stuff, I thought that would have been evident in the code I supplied. I've confirmed and logged already that I am getting back the incident number, the field "u_major_incident_task_type" is returning the expected value. however the query itself returns "[object GlideRecord]". As I said before the If Statement does not resolve as the code appears to stop running at that point. I also want this BR to run based off the current incident task, as this is the only incident task that will ever trigger the BR to run. if anything else is open, we should stop submission of the request.

 

here's the code itself if you want to actually go through it.

(function executeRule(current, previous /*null when async*/ ) {
 
    // Check if the incident task task type is 'VP Review'
    if (current.u_major_incident_task_type == '4') {
//querry the Incident task table for other open tasks related to the same incident.
       var incidentTask = new GlideRecord ('incident_task');
gs.log(current.u_major_incident_task_type, 'NRW3');
incidentTask.addQuery('incident', current.incident.number);
gs.log(current.incident.number, 'NRW1');
incidentTask.addEncodedQuery('stateNOT IN3,4,7');
incidentTask.query();
gs.log(incidentTask, 'NRW2');
 
//if other open tasks are found, prevent closure of the current task
if (incidentTask.hasNext()){
current.setAbortAction(true);
gs.addErrorMessage('All RCA tasks must be closed before closing the VP Review task.');
}
    }
 
 
})(current, previous);

Ankur Bawiskar
Tera Patron
Tera Patron

@nick135 

since incident field is reference you should use current.incident

update line 7 as this current.incident

Also did you check your incident task record has incident field populated with parent or parent field

AnkurBawiskar_0-1687439788106.png

 

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

nick135
Tera Expert

I was able to fix the code, below is the finished working product.

 

(function executeRule(current, previous /*null when async*/ ) {
 
    // Check if the incident task task type is 'VP Review'
    if (current.u_major_incident_task_type == '4') {
        //querry the Incident task table for other open tasks related to the same incident.
        var incidentTask = new GlideRecord('incident_task');
        var task = current.number;
var inc = current.incident;
 
        incidentTask.addQuery('incident', inc);
        incidentTask.addQuery('number', '!=', task);
        incidentTask.addEncodedQuery('stateNOT IN3,4,7');
        incidentTask.query();
      
        //if other open tasks are found, prevent closure of the current task
        if (incidentTask.hasNext()) {
            current.setAbortAction(true);
            gs.addErrorMessage('All RCA tasks must be closed before closing the VP Review task.');
        }
    }
 
 
})(current, previous);