- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 03:28 AM - edited 03-13-2023 03:30 AM
Hi All,
We have a requirement where we need to close the parent ticket when all the related child tickets are closed.
Wrote the below After / Update Business Rule and it's working fine only when only child ticket.
But when multi child tickets are available under one parent ticket, Currently the parent ticket is closing after the first child ticket is closed.
BR running on "rm_project" table
conditons: state changes to closed.
Script:
(function executeRule(current, previous /*null when async*/ ) {
var state1 = new GlideRecord('dmn_demand');
state1.addQuery('sys_id', current.demand);
state1.addEncodedQuery('type=scrum_epic^close_demand=On Epic Completion^active=true^state!=9');
state1.query();
while (state1.next()) {
state1.state = '9';
state1.update();
}
})(current, previous);
Advance thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 04:40 AM
Hi @LaraReddy ,
Try below script on after business rule on your child table.
(function executeRule(current, previous /*null when async*/ ) {
// get associated records related to the same parent
var grRm = new GlideRecord('rm_project');
grRm.addQuery('demand', current.demand); // Records related to same demand
grRm.addQuery('state', '!=', '9'); // get other than close state records
grRm.query();
if (!grRm.next()) { //if other states don't have any records then close parent
// Close the parent demand
var grDmd = new GlideRecord('dmn_demand');
grDmd.addQuery('sys_id', current.demand);
grDmd.query();
if (grDmd.next()) {
grDmd.state = '9';//give closed state value
grDmd.update();
}
}
})(current, previous);
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 04:00 AM
you have to check how many child tickets are open first.
var count =0;
var gr = new GlideRecord('rm_project');
gr.addEncodedQuery('state!=closed^parent='+current.demand);
gr.query();
while(gr.next())
{
count=count+1;
}
if(count==0)
{
var state1 = new GlideRecord('dmn_demand');
state1.addQuery('sys_id', current.demand);
state1.addEncodedQuery('type=scrum_epic^close_demand=On Epic Completion^active=true^state!=9');
state1.query();
while (state1.next()) {
state1.state = '9';
state1.update();
}
}
If my answer solved your issue, please mark my answer as ✅Correct & 👍Helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 04:16 AM
Hi,
I will recommend to you to use a Workflow as that will be the easiest way to handle such scenario. You have to use the "Wait for Condition" in your workflow, and if all the Child tasks are Completed/Closed, then your Parent can also be closed using Set Values.
Workflow will be Parent table.
If my answer solved your issue or helps you in any way, please mark my answer as
Correct OR
Helpful
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 04:21 AM - edited 03-13-2023 04:30 AM
Hi @LaraReddy ,
You have to get the other records that are not in closed state, if you have no records returned for that, then you can close the demand task.
Try the following code in your business rule,
(function executeRule(current, previous /*null when async*/ ) {
var rmGr = new GlideRecord(current.getTableName());
rmGr.addQuery('parent', current.parent);
rmGr.addEncodedQuery('state!=' + current.state);
rmGr.addEncodedQuery('sys_id!=' + current.sys_id);
rmGr.query();
if(!rmGr.next()){
var state1 = new GlideRecord('dmn_demand');
state1.addQuery('sys_id', current.demand);
state1.addEncodedQuery('type=scrum_epic^close_demand=On Epic Completion^active=true^state!=9');
state1.query();
while (state1.next()) {
state1.state = '9';
state1.update();
}
}
})(current, previous);
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2023 04:40 AM
Hi @LaraReddy ,
Try below script on after business rule on your child table.
(function executeRule(current, previous /*null when async*/ ) {
// get associated records related to the same parent
var grRm = new GlideRecord('rm_project');
grRm.addQuery('demand', current.demand); // Records related to same demand
grRm.addQuery('state', '!=', '9'); // get other than close state records
grRm.query();
if (!grRm.next()) { //if other states don't have any records then close parent
// Close the parent demand
var grDmd = new GlideRecord('dmn_demand');
grDmd.addQuery('sys_id', current.demand);
grDmd.query();
if (grDmd.next()) {
grDmd.state = '9';//give closed state value
grDmd.update();
}
}
})(current, previous);
ServiceNow Community MVP 2024.
Thanks,
Pavankumar