- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:09 AM
Hi,
I am closing RITM when its related sc_task is completed from below BR which is on sc_task table. I want to close REQ once all related RITM is closed(there will be multiple RITM). Does before BR will help to achieve this? OOB BR is not working in my case.
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord('sc_req_item');
gr.addQuery('sys_id', current.request_item);
gr.query();
if (gr.next()) {
gr.state = '3';
gr.stage = 'Completed';
gr.update();
}
})(current, previous);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:39 AM
@Karishma Dubey Please update your business rule script as follows.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideAggregate('sc_req_item');
gr.addQuery('request', current.request);
gr.addAggregate('COUNT');
gr.query();
var count=0;
if (gr.next()) {
count=gr.getAggregate('COUNT'); //Get all task count
}
var glideRITM = new GlideAggregate('sc_req_item');
glideRITM.addQuery('request',current.request);
glideRITM.addQuery('state','3'); //Get Closed complete task count
glideRITM.addAggregate('COUNT');
glideRITM.query();
var closedTaskCount=0;
if (glideRITM.next()) {
closedTaskCount=glideRITM.getAggregate('COUNT');
if(count==closedTaskCount){
var glideRequest=current.request.getRefRecord();
glideRequest.setValue('state','closed_complete'); //close the request if count of all task and closed task same.
glideRequest.update();
}
}
})(current, previous);
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:28 AM
Why you need BR, OOTB if RITM get closed, REQ get closed automatically? It is via flow. please check OOTB logic.
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:38 AM
Hi Atul
oob is not working
I am trying to close the ritm once sc task close and setting the stage from above BR to complete. Now I want to close req once all ritm close as OOB not working here
i think stage is not setting correctly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:28 AM
There is an OOTB Business Rule that will take care of this, see the 'Close Parent if Required' BR on the sc_req_item table. It only fires when the STAGE (not State) changes to Completed. You will need to set the Stage value of the RITM when closing it from the SC_TASK in your existing functionality. Typically, you would set the Stage to Completed in the Flow / Workflow after all Tasks have been completed instead of a Business Rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-12-2024 09:35 AM
Hi
i am creating ritm from flow with the help of for each action. Hence I am not able to close the task/RITM and set stage from flow.
That’s why I am trying to achieve this from BR.OOB BR works on stage. I am setting the stage in this BR but it is not working.