- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 12:50 AM
HI,
I have one question..
When all the child records are closed, auto close the parent record if the parent record is in state “On Hold”.
Any help will be thankfull.
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 10:58 AM
Hi @purnendutiw ,
I tried your problem in my PDI and it worked for me please check solution below
Create after business rule on particular table(I take incident table) and add below code
(function executeRule(current, previous /*null when async*/) { // Add your code here gs.log("Current State = " + current.state); if(current.state = 3){ // On Hold state gs.log("Inside if = " + current.state); var parentInc = new GlideRecord('incident'); parentInc.addQuery('state', 3); parentInc.query(); if(parentInc.next()){ gs.log("Inside Parent inc"); var childInc = new GlideRecord('incident'); childInc.addQuery('parent_incident', current.sys_id); childInc.addQuery('state', 7);//close childInc.query(); if(childInc.next()){ gs.log("Inside Child Inc While"); parentInc.state = 7; parentInc.close_code = "Duplicate"; parentInc.close_notes = "test"; parentInc.update(); gs.log("After update"); } } } })(current, previous);
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 08:03 AM
you can use after update BR on that table
Sample script below
(function executeRule(current, previous /*null when async*/ ) {
// Check if the current record is a child record and is being closed
if (current.state == 'Closed') {
var parentGR = new GlideRecord('parent_table'); // Replace 'parent_table' with the actual parent table name
if (parentGR.get(current.parent)) { // Replace 'parent' with the actual reference field to the parent record
// Check if the parent record is in "On Hold" state
if (parentGR.state == 'On Hold') {
var childGR = new GlideRecord('child_table'); // Replace 'child_table' with the actual child table name
childGR.addQuery('parent', parentGR.sys_id); // Replace 'parent' with the actual reference field to the parent record
childGR.addQuery('state', '!=', 'Closed');
childGR.query();
if (!childGR.hasNext()) {
// All child records are closed, update the parent record state to "Closed"
parentGR.state = 'Closed';
parentGR.update();
}
}
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 09:32 AM
This Business Rule should be set to run whenever a child record is updated. (After - Update - Business rule)
// Business Rule script
(function executeRule(current, previous /*null when async*/) {
//Query to find all the 'child' records associated with the 'parent' record
var childGR = new GlideRecord('child_table');
childGR.addQuery('parent', current.parent);
childGR.query();
var allClosed = true;
while (childGR.next()) {
if (childGR.state != 'closed') { // Replace 'closed' with the field value signifying a closed state in your instance
allClosed = false;
break;
}
}
// If all child records are closed
if(allClosed == true){
// Call function to check and close parent record
closeParent(current.parent);
}
function closeParent(parent_id) {
var parentGR = new GlideRecord('parent_table_name'); // Replace 'parent_table_name' with the actual name of your parent table
if(parentGR.get(parent_id) && parentGR.state=='On Hold') { // Replace 'On Hold' with the field value corresponding to On Hold state in your instance
parentGR.state = 'closed'; // Replace 'closed' with the field value corresponding to closed state in your instance
parentGR.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 10:31 AM
Hi @purnendutiw ,
I tried your problem in my PDI and it worked for me please check solution below
Create after business rule on particular table(I take incident table) and add below code
(function executeRule(current, previous /*null when async*/) {
// Add your code here
gs.log("Current State = " + current.state);
if(current.state = 3){ // On Hold state
gs.log("Inside if = " + current.state);
var parentInc = new GlideRecord('incident');
parentInc.addQuery('state', 3);
parentInc.query();
if(parentInc.next()){
gs.log("Inside Parent inc");
var childInc = new GlideRecord('incident');
childInc.addQuery('parent_incident', current.sys_id);
childInc.addQuery('state', 7);//close
childInc.query();
if(childInc.next()){
gs.log("Inside Child Inc While");
parentInc.state = 7;
parentInc.close_code = "Duplicate";
parentInc.close_notes = "test";
parentInc.update();
gs.log("After update");
}
}
}
})(current, previous);
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-28-2024 10:35 AM
Hi @purnendutiw ,
I tried your problem in my PDI and it worked for me please check solution below
Create after business rule on particular table(I take incident table) and add below code
(function executeRule(current, previous /*null when async*/) { // Add your code here gs.log("Current State = " + current.state); if(current.state = 3){ // On Hold state gs.log("Inside if = " + current.state); var parentInc = new GlideRecord('incident'); parentInc.addQuery('state', 3); parentInc.query(); if(parentInc.next()){ gs.log("Inside Parent inc"); var childInc = new GlideRecord('incident'); childInc.addQuery('parent_incident', current.sys_id); childInc.addQuery('state', 7);//close childInc.query(); if(childInc.next()){ gs.log("Inside Child Inc While"); parentInc.state = 7; parentInc.close_code = "Duplicate"; parentInc.close_notes = "test"; parentInc.update(); gs.log("After update"); } } } })(current, previous);
Result
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak