JavaScript example: Limiting the Assigned to field

This example shows how to restrict an incident's Assigned to choices to only the users with the itil_admin role.

About this task

You could also change itil_admin to any other role on a reference field that refers to the User table.

Procedure

  1. Open an incident.
  2. Right-click the Assigned to field and select Configure Dictionary.
  3. In the Reference qual field, enter javascript:"sys_idIN"+getRoledUsers("itil_admin").join(",") .
  4. Save the record.
  5. To see the base-system business rule that this JavaScript code calls, navigate to System Definition > Business Rules.
  6. Open getRoledUsers.

    The business rule uses the following JavaScript code.
    // Return an array of sys_ids of the users that have at least one role
    // optional parameters allow the exclusion (NOT IN) of some roles or
    // look for specific roles (IN)
    //
    // optional: queryCondition - 'IN' or 'NOT IN'
    // optional: roleList - a comma separated list of role names
    //
    function getRoledUsers(queryCondition, roleList) {
       var roleListIds;
       if (queryCondition && roleList) {
          roleListIds = getRoleListIds(roleList);
       }
    
       var users = {};
       var gr = new GlideRecord('sys_user_has_role');
       if (roleListIds) {
          gr.addQuery('role', queryCondition, roleListIds);
       }
       gr.query();
       while (gr.next()) {
          users[gr.user.toString()] = true;
       }
       
       var ids = [];
       for (var id in users)
          ids.push(id);
          
       return ids;
    }
    
    // get sys_id's for the named roles
    function getRoleListIds(roleList) {
       var ids = [];
       var gr = new GlideRecord('sys_user_role');
       gr.addQuery('name','IN',roleList);
       gr.query();
       while (gr.next()) {
          ids.push(gr.sys_id.toString());
       }
       return ids;
    }