Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Set Value with Business Rule when all child records are Closed

rohiniv
Tera Contributor

Hi All,

I want to set one field on parent table when all the child records from custom tables are closed with business Rule.

I need to check each and each record has been closed or not.

 

Can anyone help me with this.

Thanks & Regards,

Rohini

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @rohiniv 

 

Might be give you an idea

 

https://www.servicenow.com/community/developer-forum/setting-field-value-on-parent-table-when-child-...

*************************************************************************************************************
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]

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

swathisarang98
Giga Sage

Hi @rohiniv ,

 

you can try with the below code ,

Here i have taken RITM table as parent and child as sc task make the changes according to your requirement,

 

 

var gr = new GlideRecord("sc_req_item");
gr.addEncodedQuery("state=1"); // state is open
gr.query();
while(gr.next()) {
	var closedCount = 0;
	var taskRec = new GlideRecord("sc_task");
	taskRec.addQuery("request_item", gr.sys_id);
	taskRec.query();
	var totalCount = taskRec.getRowCount();
	while(taskRec.next()){
			if(taskRec.active.toString() == 'false')
                    {
					closedCount++;
				    }
			            }

			if(closedCount == totalCount && totalCount != 0)
            {
				gr.state = '3'; // update the parent field 
                                gr.update();
			}
		}
		

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang