When all the child records are closed, auto close the parent record if the parent record is in state

purnendutiw
Tera Contributor

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.

1 ACCEPTED SOLUTION

sarthakkash
Kilo Guru

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);

 

sarthakkash_0-1735412258553.png

 

 

 

sarthakkash_1-1735412258607.png

 

 

 

Result 

sarthakkash_2-1735412258564.png

 

 

sarthakkash_3-1735412258611.png

 

 

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@purnendutiw 

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.

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

rambo1
Tera Guru

@purnendutiw 

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);

Sarthak Kashyap
Tera Expert

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);

 

SarthakKashyap_0-1735410577492.png

 

SarthakKashyap_1-1735410595738.png

 

Result 

SarthakKashyap_2-1735410631567.png

SarthakKashyap_3-1735410669715.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

Sarthak Kashyap
Tera Expert

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);

 

SarthakKashyap_0-1735410897190.png

 

 

SarthakKashyap_1-1735410897197.png

 

 

Result 

SarthakKashyap_2-1735410897200.png

 

SarthakKashyap_3-1735410897202.png

 

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak