question in Business Rule code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi Community,
I am new to ServiceNow Business Rules and I am trying to understand this script. I understand basic GlideRecord, but I am struggling to understand the flow of this code.
Can someone please explain the execution flow step by step?
My doubts are:
1. Why are we using `sys_user_group` first and then `sys_user_grmember` and `sys_user` tables?
2. What is the purpose of `group.sys_id` in `member.addQuery('group', group.sys_id)`?
3. How does `member.user` connect with the `sys_user` table?
4. Why do we use `while(group.next())` and `while(member.next())` here?
5. Is this the correct approach to automatically assign an assignment group and user based on category?
Code:
(function executeRule(current, previous /*null when async*/) {
if (current.category == 'network') {
var group = new GlideRecord('sys_user_group');
group.addQuery('name', 'network');
group.query();
while (group.next()) {
current.assignment_group = group.sys_id;
var member = new GlideRecord('sys_user_grmember');
member.addQuery('group', group.sys_id);
member.query();
while (member.next()) {
var uservar = new GlideRecord('sys_user');
if (uservar.get(member.user)) {
if (uservar.active == true) {
current.assigned_to = uservar.sys_id;
}
}
}
}
}
})(current, previous);
I would appreciate an explanation of the logic/flow rather than only code correction.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
57m ago - last edited 56m ago
Use case with example (from the code , how it is working)
Suppose:
- Checking if Category = Network
- Network group contains:
- John (active)
- Mary (active)
- Beth (inactive)
The script:
- Assigns the ticket to the Network Assignment group.
- Loops through Network Assignment group's group member i.e John → assigns ticket to John.
- Loops through Mary → reassigns ticket to Mary.
- Skips Beth because inactive.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti