Population of users based on entities

harinya
Tera Contributor

Hi 
can some one help on your suggestion 
there is field called users which is list collector and Entities field (List Collector), Based on the selected Entities, the Users field should display/populate only the users who are already assigned for those entities in another table(entity stakeholder) i found one on change client script if there is any other possibilities please give your suggestions.
Thanks in Advance

1 REPLY 1

Tanushree Maiti
Tera Patron

Hi @harinya ,

 

With you onChange client script, Add this .

1 ) Create a Script Include

Navigate to System Definition > Script Includes and create a new record. This script processes the selected entities and queries your mapping table. [1]

  • Name: UsersByEntity
  • Client callable: true

 

Sample code/not tested:

 

var UsersByEntity = Class.create();

UsersByEntity.prototype = {

    initialize: function() {},

    getUsersForEntities: function(entityIds) {

        if (!entityIds) return 'sys_idIN';

        var entityArray = entityIds.split(',');

        var userIds = [];

        var gr = new GlideRecord('u_entity_user_mapping'); //Replace your entity table name

        gr.addQuery('u_entity', 'IN', entityArray);

        gr.query();       

        while (gr.next()) {

            userIds.push(gr.getValue('u_user')); // Replace 'u_user' with your actual user reference field

        }

        if (userIds.length > 0) {

            return 'sys_idIN' + userIds.join(',');

        } else {

            return 'sys_idIN';

        }

    },

    type: 'FilterUsersByEntity'

};

 

2) Open your catalog item variables and configure the Users list collector variable.

  • Go to the Type Specifications tab.
  • Set Reference to User [sys_user].
  • Set Reference qualifier to Advanced.
  • Paste the following logic into the Reference qual field:
    javascript&colon;new UsersByEntity().getUsersForEntities(current.variables.<NAME_OF_ENTITIES_FIELD>);
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