Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

in servicenow, catalog item, i have a reference choice field from sysusergroup table and…

  1. 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

  2. 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:

    1. 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).

    Step-by-Step Instructions:

    1. 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.
    2. Add the Reference Fields:

      • In the catalog item form, go to the Variables section:
        • Add a new variable of type Reference and set the reference table to sys_user_group. This will be the Group field.
        • Add another variable of type Reference and set the reference table to sys_user. This will be the Group Members field.
    3. Set Up the Client Script:

      • You will need a Client Script to filter the Group Members field based on the selected Group. Here’s an example of how you can do this:
    // 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(',');
    }
    
    1. Associate the Client Script to the Fields:

      • Add an onChange Client Script for the Group field.
      • 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 Group field value changes.
    2. Test the Configuration:

      • Load the catalog item form in the Service Catalog.
      • Select a group in the Group field.
      • Check if the Group Members field 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, and sys_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.

  3. User

    i want that if i select one group thaen i will only see that group members only in my user reference field variable

  4. 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

    1. 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 Group and Group Members fields.
    2. 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
    3. Set Up the Client Script:

      • You need an onChange Client Script for the Group field that dynamically filters the options of the Group Members field. Here’s an example:
    // 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(',');
    }
    
    1. 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 onChange Client 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);
        });
    }
    
    1. Test the Configuration:

      • Navigate to your Service Catalog and open the catalog item form.
      • Select a group in the Group field.
      • Verify that the Group Members field dynamically updates to show only the users who are members of the selected group.

    By following these instructions, you ensure that the Group Members reference field in your catalog item only displays users belonging to the selected group, fulfilling your requirement.

  5. User

    catalog client script on change group

  6. Assistant

    To achieve the requirement where the Group Members field dynamically updates based on the selected Group in a ServiceNow catalog item, you'll need to use a Catalog Client Script that listens for changes on the Group field.

    Here's how you can set it up step-by-step:

    Step-by-Step Instructions

    1. 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 Group and Group Members fields.
    2. 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
    3. 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);
        });
    }
    
    1. Create a Script Include:
      • Navigate to System Definition > Script Includes.
      • Create a new Script Include named GroupMembersUtil and 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(',');
        }
    });
    
    1. Test the Configuration:
      • Navigate to your Service Catalog and open the catalog item form.
      • Select a group in the Group field.
      • Verify that the Group Members field 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 Members field based on the selected Group in your catalog item in ServiceNow.