- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 10:35 PM
I have a requirement to prevent story closer if defect is still active.
I tried with writing BR but not working.
(function executeRule(current, previous /*null when async*/) {
// Query the defect table for associated defects
var defectGr = new GlideRecord('rm_defect'); // Replace 'rm_defect' with the actual defect table name
defectGr.addQuery('story', current.sys_id); // Assuming 'story' is the reference field on the defect table
defectGr.addQuery('state', '!=', 'Closed'); // Replace 'state' and 'Closed' with actual field and value
defectGr.query();
if (defectGr.hasNext()) {
// Prevent the story from being closed
gs.addErrorMessage('This story cannot be closed because it has associated defects that are still open.');
current.state = previous.state; // Revert the state change
gs.log('Story closure prevented due to open defects: ' + current.number, 'Story Closure Rule');
}
})(current, previous);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:14 PM
Hi @Prakash_S ,
You can achieve it via configuration of BR.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:20 PM
Hi @Prakash_S ,
Why don't use defect field on the story to check if still active and abort action.
You don't need to any scripting logic
Step 1)
step 2)
Hope this helps!!!
Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:00 PM
Hello Prakash,
You have not specified which table this BR is running on, assuming it is on Story table.
Use below instead of trying to revert state to previous state.
Your BR should also run on before so state change can be prevented.
Additional improvement, use GlideAggregate instead of GlideRecord for performance reasons.
current.setAbortAction(true);
Br, Abhishek
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:06 PM
Hi @Prakash_S
Please appreciate the efforts of community contributors by marking the appropriate response as the correct answer and helpful. This may help other community users to follow the correct solution in the future.
********************************************************************************************************
Cheers,
Prashant Kumar
ServiceNow Technical Architect
Community Profile LinkedIn YouTube Medium TopMate
********************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:11 PM
Why if condition in code "if (current.state.changesTo('closed'))" ?
This should be in the filter condition field for BR to execute or in Condition field, not in script body.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2024 11:14 PM
Hi @Prakash_S ,
You can achieve it via configuration of BR.
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------