- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 10:15 AM
Hello Community,
I have a question about calling a Script Include in a business rule.
I have a list containing numerous groups, not confined to a specific table; a user forwarded this list to me via email showing me the groups he want me to use.
I need to add a true/false button to the task form, and this button should be set to 'true' whenever a user from one of the groups in the list accesses the task form.
I created a business rule that will set the button to true, and I need to build a condition similar to
gs.getUser().isMemberOf('group')
But the problem is: I have a LOT of groups! An about 60 groups.
So I made a Script Include
My intention is to verify if the current user is member of one of the groups in the array and if this is true, then the true/false button will be set to true.
In my tests every time I reload the task form, it sets the button to true, even if the user is not in any of the groups.
I think I'm doing something wrong in the condition field on business rule, because I don't know exactly how to use it.
Should I call the Script Include via Condition field in the business rule or do it via Script?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 01:35 PM
Hardcoding groups is not a best practice. I would either create a field in the group table to indicate that or store the groups in a system property record. Imagine, if you have to change that group list frequently, you can't change the code everytime. Instead you can make changes to the property to add the group.
You can add the validation in the script itself to validate the groups.
In the script you can do gs.getProperty('<property name>') and validate if the current user is member of any of the groups. Also instead of looping through the PSAGroup, loop through the UserGroup. That way the number of loops will be less.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 01:17 PM - edited ‎12-13-2023 01:27 PM
The condition field is an option to provide a Scripted condition to your Business Rule if you can't write the condition using the Condition Builder. ServiceNow expects JavaScript here, so you can call your function that returns true or false in the condition. If the result is FALSE from your function the Business Rule will not execute, if it is TRUE, it will.
The code I provided is just an example, but you can use it and modify it further to your needs. In the case where you want the Business Rule to run in the negative (false) scenario, just add ! before the code in the condition.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 01:25 PM
Hi, if the script-include function returning true is a trigger condition for the BR to run, then yes I probably would add it as a condition as this could make for easier debugging in future. However as a solution, if the only requirement of this functionality is to evaluate if a user is a member of 1 of many groups, I would also consider a custom field true\false on sys_user that reflects membership of these groups, so that platform resource is minimized.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-13-2023 01:35 PM
Hardcoding groups is not a best practice. I would either create a field in the group table to indicate that or store the groups in a system property record. Imagine, if you have to change that group list frequently, you can't change the code everytime. Instead you can make changes to the property to add the group.
You can add the validation in the script itself to validate the groups.
In the script you can do gs.getProperty('<property name>') and validate if the current user is member of any of the groups. Also instead of looping through the PSAGroup, loop through the UserGroup. That way the number of loops will be less.
Please mark this response as correct or helpful if it assisted you with your question.