- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 04:30 AM
Hi everyone
Please see if you can see what is wrong with my code. I created a business rule that should hide the 'high priority' option on the incident form for all users except those in the BST-Service Desk group. It does not seem to work? Only Service Desk should be able to raise the priority to P1 (High).
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 08:39 AM - edited 07-16-2024 09:46 AM
Hi @Thereza Van der,
Thanks for confirming. As an FYI and something you should be mindful of is the Priority lookup Rules. It's here where the Priority is calculated from a matrix of Urgency and Impact values.
To view these, type and select: Priority lookup Rules in the In the Navigation menu.
If group membership were not an issue, you could simply update the Lookup rule that when Urgency and Impact are '1 - High', the Priority could be set to '2 - High' (Rather than the Out Of Box default of '1 - Critical').
No code would be needed, however, group membership is in context so we will need to use a Script Include and Client Script with a slight tweak to what was provided before.
Rather than remove the option of Priority 1, as this is a read only and a calculated value, let's remove the impact value of 1 which essentially means based on the Priority Lookup Rules, if Impact of 1 can not be selected, then the Priority can never be 1 (Assuming the Lookup rules have not been changed)
Client Script:
function onLoad() {
var priorityField = 'impact'; // Change this to 'impact' if you want to test on another field which should be editable (OOB config)
var groupName = 'BST-Service Desk'; // Rather than hardcoding, can we use a form field such as Assignment Group to make this dynamic?
var sysid = g_user.userID; //get current user sysid
var userGroupGA = new GlideAjax('global.CheckUserGroup'); //script include name
userGroupGA.addParam('sysparm_name', 'getgroup'); //function name
userGroupGA.addParam('sysparm_name_sysid', sysid); //passing sysid to server
userGroupGA.addParam('sysparm_name_groupName', groupName); //passing group name to server
userGroupGA.getXMLAnswer(getGroup);
function getGroup(response) {
if (response == 'true') {
g_form.addInfoMessage('Part of group');
//g_form.addOption(priorityField, '1', '1 - High'); // You don't need to add this as it's part of the default list, you only need to remove it in the else
} else {
g_form.addInfoMessage('Not Part of group');
g_form.removeOption(priorityField, '1');
}
}
}
Script Include: (The same as before, but just in case)
var CheckUserGroup = Class.create();
CheckUserGroup.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getgroup: function() {
var usersysid = this.getParameter('sysparm_name_sysid');//getting usersy sid from client
var groupName = this.getParameter('sysparm_name_groupName');//get group from client (Dynamic and should be used)
var mem = new GlideRecord("sys_user_grmember");
mem.addQuery('user', usersysid); //filtering current user
mem.addQuery('group.name', groupName);
mem.query();
if (mem.next()) {
return true;
} else {
return false;
}
},
type: 'CheckuserGroup'
});
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 04:32 AM
@Thereza Van der The code you have posted is client side code and should be a part of a client script. This code will not work inside a business rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:06 AM
I am sorry Sandeep, I have been battling with this for 2 days but cannot get it working. I meant client script. I created 3 different business rules, 4 client scripts, a UI policy, nothing seems to work. On days like these I don't enjoy ServiceNow at all although I love it in general. Can you see the problem on the code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 04:37 AM
Hi @Thereza Van der,
please use client script:
onLoad client script:
function onLoad() {
// Call the function to show/hide high priority
showHighPriorityForBSTServiceDesk();
}
function showHighPriorityForBSTServiceDesk() {
var userGroups = g_user.getMyGroups(); // Get user's groups
var priorityField = 'priority'; // Priority field name
// Check if the user belongs to the "BST-Service Desk" group
if (userGroups.includes('BST-Service Desk')) {
// Show the high-priority option (e.g., add it back to the choice list)
g_form.addOption(priorityField, '1', '1 - High');
} else {
// Hide the high-priority option
g_form.removeOption(priorityField, '1');
}
}
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:00 AM
You mentioned business rules which is server-side script but your code is Client Script. So I believe you want to achieve this business objective via a client script
Potential Issues
1. Client-Side Scripting: The code uses g_user and g_form, which are ServiceNow client-side scripting objects. Ensure the business rule is set to run on the client-side.
2. Group Membership Check: The way userGroups.includes('BST-Service Desk') checks for group membership might not be the most reliable method in ServiceNow.
3. Hidden Field Removal: In some cases, if the high priority option was initially hidden by a UI Policy, removing the field using g_form.removeOption() might not work.
4. Another important is pls check your table field Display is True otherwise you see sYS ID Code instead on Name
function showHighPriorityForBSTServiceDesk() {
var isBSTServiceDeskMember = g_user.isInGroup('BST-Service Desk');
var priorityField = g_form.getControl('priority');
if (isBSTServiceDeskMember) {
g_form.setVisible(priorityField, true);
g_form.addOption(priorityField, '1', '1 - High');
} else {
g_form.removeOption(priorityField, '1');
// Optionally, hide the field if empty after removing '1 - High'
if (g_form.getOption(priorityField) == null) {
g_form.setVisible(priorityField, false);
}
}
}
Important Considerations:
* GlideAjax: If your ServiceNow instance is very old or highly customized, there's a slight chance this method might not be available. In those rare cases, you can use GlideAjax to call a server-side script that checks group membership and returns the result to the client.
* Best Practices: Even though g_user.isInGroup() is a valid client-side method, using server-side checks for group membership is often considered a better practice from a security perspective. However, for many simple scenarios, g_user.isInGroup() can be a convenient and efficient solution.
A small request from my end, If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer (Accepted Solution) 🌠 OR mark it Helpful ⛑ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community