We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

question in Business Rule code

GouthamiK783388
Tera Contributor

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.

1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @GouthamiK783388 

 

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.
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

2 REPLIES 2

Tanushree Maiti
Tera Patron

Hi @GouthamiK783388 

 

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.
Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Kieran Anson
Kilo Patron

5. Is this the correct approach to automatically assign an assignment group and user based on category?

 

It "works" but there are alternative / approaches. This code isn't efficient, and is fragile. If the network group is ever renamed then it will fail. It also adds a lot of boilerplate code that could be simplified and made more readable