in servicenow, catalog item, i have a reference choice field from sysusergroup table and…
- User
in servicenow, catalog item, i have a reference choice field from sys_user_group table and my requirement is when i select any group then in second field all the members will show of that group
- Assistant
In ServiceNow, you can dynamically filter the options available in a reference field based on the value selected in another reference field. For your specific requirement, you want to filter the members of a group based on the group selected in a catalog item form.
Here’s how you can achieve this:
-
Create the Reference Fields in the Catalog Item:
- First, create a reference field for the group (let's call it
Group). - Then, create a reference field for users (let's call it
Group Members).
- First, create a reference field for the group (let's call it
Step-by-Step Instructions:
-
Create or Edit the Catalog Item:
- Navigate to Service Catalog > Catalog Definitions > Maintain Items.
- Open or create the catalog item where you want to add these fields.
-
Add the Reference Fields:
- In the catalog item form, go to the
Variablessection:- Add a new variable of type
Referenceand set the reference table tosys_user_group. This will be theGroupfield. - Add another variable of type
Referenceand set the reference table tosys_user. This will be theGroup Membersfield.
- Add a new variable of type
- In the catalog item form, go to the
-
Set Up the Client Script:
- You will need a Client Script to filter the
Group Membersfield based on the selectedGroup. Here’s an example of how you can do this:
- You will need a Client Script to filter the
// Client Script to filter 'Group Members' based on selected 'Group' function onLoad() { g_form.setValue('group', ''); g_form.setValue('group_members', ''); g_form.clearOptions('group_members'); } function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; //No need to proceed further if form is loading or no value selected } // Reference to the group ID selected in 'Group' var groupID = g_form.getValue('group'); // Prepare query to filter users who are members of the selected group var groupMembersQuery = 'sys_idIN' + getGroupMembers(groupID); // Apply filter to the 'Group Members' field g_form.setDependent('group_members', groupMembersQuery); } // Helper function to fetch group members' sys_ids function getGroupMembers(groupID) { var userGR = new GlideRecord('sys_user_grmember'); userGR.addQuery('group', groupID); userGR.query(); var userIds = []; while (userGR.next()) { userIds.push(userGR.user.sys_id.toString()); } return userIds.join(','); }-
Associate the Client Script to the Fields:
- Add an
onChangeClient Script for theGroupfield. - Inside the script, ensure the section responsible for fetching and filtering group members is present.
- You might also want to handle the scenario when the form is loaded or the
Groupfield value changes.
- Add an
-
Test the Configuration:
- Load the catalog item form in the Service Catalog.
- Select a group in the
Groupfield. - Check if the
Group Membersfield dynamically updates to show only the users from the selected group.
Notes:
- Ensure you have appropriate ACLs (Access Control Lists) configured for the involved tables (
sys_user,sys_user_group, andsys_user_grmember).
This configuration allows you to filter users dynamically based on the selected group within a catalog item in ServiceNow. If more customization or error handling is needed, you might need to modify the client script accordingly.
-
- User
i want that if i select one group thaen i will only see that group members only in my user reference field variable
- Assistant
To ensure that selecting a group in your catalog item will dynamically update the user reference field to show only members of the selected group, follow these steps:
Step-by-Step Instructions
-
Create or Edit the Catalog Item:
- Navigate to Service Catalog > Catalog Definitions > Maintain Items.
- Open or create the catalog item where you want to add the
GroupandGroup Membersfields.
-
Add the Reference Fields:
- In the catalog item form, go to the Variables section:
-
Add a new variable:
- Type: Reference
- Name: Group
- Question Text: Group
- Reference Table:
sys_user_group
-
Add another variable:
- Type: Reference
- Name: Group Members
- Question Text: Group Members
- Reference Table:
sys_user - Reference Qual: Dynamic
-
- In the catalog item form, go to the Variables section:
-
Set Up the Client Script:
- You need an
onChangeClient Script for theGroupfield that dynamically filters the options of theGroup Membersfield. Here’s an example:
- You need an
// Client Script to filter 'Group Members' based on selected 'Group' function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; // No need to proceed if form is loading or no value selected } // Reference to the group ID selected in 'Group' var groupID = g_form.getValue('group'); // Construct the filter condition to show only users in the selected group var filterCondition = 'sys_idIN' + getGroupMembersSysIds(groupID); // Apply filter to the 'Group Members' field g_form.setValue('group_members', ''); // Clear the Group Members field g_form.setReadOnly('group_members', false); // Enable the field if it was disabled g_form.setReferenceQualifier('group_members', filterCondition); } // Helper script include function to fetch group members' sys_ids function getGroupMembersSysIds(groupID) { var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('group', groupID); gr.query(); var userIds = []; while (gr.next()) { userIds.push(gr.user.sys_id.toString()); } return userIds.join(','); }-
Create a Script Include (Optional but Recommended for Better Performance):
-
Navigate to System Definition > Script Includes and create a new Script Include to handle the retrieval of user IDs. This improves performance and keeps your Client Script cleaner.
-
Add a new Script Include with the following code:
-
var GroupMembersUtil = Class.create(); GroupMembersUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, { getGroupMembersSysIds: function(groupID) { var userIds = []; if (groupID) { var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('group', groupID); gr.query(); while (gr.next()) { userIds.push(gr.user.sys_id.toString()); } } return userIds.join(','); } });- Modify the
onChangeClient Script to call this Script Include:
// Client Script to filter 'Group Members' based on selected 'Group' function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; // No need to proceed if form is loading or no value selected } var groupID = g_form.getValue('group'); // AJAX call to the Script Include to fetch the group members' sys_ids var ajax = new GlideAjax('GroupMembersUtil'); ajax.addParam('sysparm_name', 'getGroupMembersSysIds'); ajax.addParam('groupID', groupID); ajax.getXMLAnswer(function(response) { var userIds = response.responseXML.documentElement.getAttribute('answer'); var filterCondition = 'sys_idIN' + userIds; // Apply filter to the 'Group Members' field g_form.setValue('group_members', ''); // Clear the Group Members field g_form.setReadOnly('group_members', false); // Ensure the field is editable g_form.setReferenceQualifier('group_members', filterCondition); }); }-
Test the Configuration:
- Navigate to your Service Catalog and open the catalog item form.
- Select a group in the
Groupfield. - Verify that the
Group Membersfield dynamically updates to show only the users who are members of the selected group.
By following these instructions, you ensure that the
Group Membersreference field in your catalog item only displays users belonging to the selected group, fulfilling your requirement. -
- User
catalog client script on change group
- Assistant
To achieve the requirement where the
Group Membersfield dynamically updates based on the selectedGroupin a ServiceNow catalog item, you'll need to use a Catalog Client Script that listens for changes on theGroupfield.Here's how you can set it up step-by-step:
Step-by-Step Instructions
-
Create or Edit the Catalog Item:
- Navigate to Service Catalog > Catalog Definitions > Maintain Items.
- Open or create the catalog item where you want to add the
GroupandGroup Membersfields.
-
Add the Reference Fields:
- In the catalog item form, go to the Variables section:
-
Add a new variable:
- Type: Reference
- Name:
Group - Question Text:
Group - Reference Table:
sys_user_group
-
Add another variable:
- Type: Reference
- Name:
Group Members - Question Text:
Group Members - Reference Table:
sys_user
-
- In the catalog item form, go to the Variables section:
-
Create the Catalog Client Script:
- Navigate to Service Catalog > Catalog Definitions > Maintain Items, open your catalog item, and scroll to the Catalog Client Scripts related list.
- Create a new Catalog Client Script.
(function executeRule(current, previous /*null when async*/) { // Variables var selectedGroupSysId = ""; // Get the group selected in the 'Group' variable selectedGroupSysId = g_form.getValue('Group'); // Prepare the filter condition for the 'Group Members' field var groupMembersFilterCondition = getGroupMembersFilterCondition(selectedGroupSysId); // Apply the filter to the 'Group Members' Reference Qualifier g_form.setValue('Group Members', ''); // Clear the 'Group Members' field g_form.setReadOnly('Group Members', false); // Ensure the field is editable g_form.setReferenceQualifier('Group Members', groupMembersFilterCondition); // Function to prepare the filter condition for fetching group members function getGroupMembersFilterCondition(groupSysId) { var groupMembersSysIds = ""; // Making an AJAX call to a Script Include to fetch the group members' sys_ids var ga = new GlideAjax('GroupMembersUtil'); ga.addParam('sysparm_name', 'getGroupMembersSysIds'); ga.addParam('groupSysId', groupSysId); groupMembersSysIds = ga.getXMLAnswer(); return "sys_idIN" + groupMembersSysIds; } })(); function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; // No need to proceed further if form is loading or no value selected } // Get the group selected in the 'Group' variable var selectedGroupSysId = g_form.getValue('Group'); // Making an AJAX call to get the filter condition for group members var ga = new GlideAjax('GroupMembersUtil'); ga.addParam('sysparm_name', 'getGroupMembersSysIds'); ga.addParam('groupSysId', selectedGroupSysId); ga.getXMLAnswer(function(response) { var groupMembersFilterCondition = response.responseXML.documentElement.getAttribute('answer'); // Apply the filter to the 'Group Members' Reference Qualifier g_form.setValue('Group Members', ''); // Clear the 'Group Members' field g_form.setReadOnly('Group Members', false); // Ensure the field is editable g_form.setReferenceQualifier('Group Members', groupMembersFilterCondition); }); }- Create a Script Include:
- Navigate to System Definition > Script Includes.
- Create a new Script Include named
GroupMembersUtiland make it accessible from client-side scripts. You can paste the following script:
var GroupMembersUtil = Class.create(); GroupMembersUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, { getGroupMembersSysIds: function() { var groupSysId = this.getParameter('groupSysId'); var userIds = []; if (groupSysId) { var gr = new GlideRecord('sys_user_grmember'); gr.addQuery('group', groupSysId); gr.query(); while (gr.next()) { userIds.push(gr.user.sys_id.toString()); } } return userIds.join(','); } });- Test the Configuration:
- Navigate to your Service Catalog and open the catalog item form.
- Select a group in the
Groupfield. - Verify that the
Group Membersfield dynamically updates to show only the users who are members of the selected group.
With these steps, you've set up a Catalog Client Script that dynamically updates the
Group Membersfield based on the selectedGroupin your catalog item in ServiceNow. -