- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2024 03:11 AM
Hi,
I had a requirement to disable the comments on some knowledge bases which I was not able to achieve by checking or unchecking the disable commenting box on the knowledge base form and it was because of this business rule below.
Can someone please explain what is going on in it? To get this business rule to work, I have to check (disable commenting) field save it, then uncheck it and save it and then check it again and save it. Why do I need to check it, uncheck it and then recheck it for it to set the comments to disable.
condition: !previous || current.disable_commenting.changes()
(function executeRule(current, previous /*null when async*/) {
var kbKnowledge = new GlideRecord("kb_knowledge");
kbKnowledge.addQuery("kb_knowledge_base", current.sys_id);
kbKnowledge.addQuery('disable_commenting','!=',current.disable_commenting);
kbKnowledge.setWorkflow(false);
kbKnowledge.query();
while (kbKnowledge.next()){
kbKnowledge.setValue("disable_commenting", current.disable_commenting);
kbKnowledge.setWorkflow(false);
kbKnowledge.autoSysFields(false);
kbKnowledge.update();
}
})(current, previous);
it is an async (deprecated: not run during upgrade)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2024 03:26 AM
Hi @snow_beginner,
This business rule appears to be designed to synchronize the value of the "disable_commenting" field across related knowledge base records when it is changed on a specific knowledge base record.
Here's what the business rule does:
1. **Condition Check**: The condition `!previous || current.disable_commenting.changes()` ensures that the business rule runs when either the record is newly created (`!previous`) or when there is a change in the "disable_commenting" field.
2. **GlideRecord Query**: It creates a GlideRecord query to fetch all knowledge base records (`kb_knowledge`) associated with the same knowledge base as the current record (`current.sys_id`).
3. **Query Filter**: Additionally, it filters the knowledge base records where the value of the "disable_commenting" field is different from the current record's "disable_commenting" field (`'disable_commenting','!=',current.disable_commenting`).
4. **Loop through Records**: It iterates through each matching knowledge base record using a `while` loop.
5. **Update Records**: For each matching knowledge base record, it sets the "disable_commenting" field to the value of the current record's "disable_commenting" field.
6. **Workflow and Audit Fields**: It disables workflows and sets `autoSysFields` to `false` to prevent unnecessary triggering of business rules and to avoid updating system fields like `sys_updated_on` and `sys_updated_by`.
The reason why you need to check, uncheck, and recheck the "disable_commenting" field for it to work as expected might be due to the way the business rule is written. It could be designed to trigger only when there is a change in the "disable_commenting" field, and performing these actions may cause the field's value to change, thus triggering the business rule.
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2024 03:27 AM
Nice such incorrect AI replies 🤣
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field