- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-28-2024 04:56 AM
In ServiceNow, a common requirement is to dynamically update the "Assigned To" field based on the selected "Assignment Group" on a UI page. The goal is to display only users belonging to the selected group.
Problem Description:
The UI page contained two reference fields:
- Assignment Group: A reference to the sys_user_group table, where users select an active group.
- Assigned To: A reference to the sys_user table, which needed to display users belonging to the selected "Assignment Group."
When the "Assignment Group" was selected, the "Assigned To" field was expected to show only the users who belonged to that group, but it wasn't functioning as intended.
Solution:
UI Page Code:
The onChange attribute for the "Assignment Group" triggers the function onAssignmentGroupChange() whenever a group is selected.
<label>Assignment Group</label>
<g:ui_reference id="assignment_group" name="assignment_group" table="sys_user_group" query="active=true" onChange="onAssignmentGroupChange()" />
<label>Assigned To</label>
<g:ui_reference id="assigned_to" name="assigned_to" table="sys_user" query="active=true" />
Client Script Code:
-
onAssignmentGroupChange() Function: This function is triggered when a new "Assignment Group" is selected. It first retrieves the selected group's value using gel('assignment_group').value, which refers to the internal ID of the group.
-
GlideAjax Call: A GlideAjax object is created to call the server-side Script Include. The ga.addParam('sysparm_group', assignGroup) line sends the selected group ID to the server via the sysparm_group parameter.
-
getXML() Method: This method sends the request to the Script Include and, upon receiving a response, calls the handleXml() function to process the returned data.
-
Processing the Response: The handleXml function processes the response from the server. The response.responseXML.documentElement.getAttribute("answer") extracts the list of user IDs returned from the Script Include. These IDs represent the users in the selected group.
-
Updating the Lookup Attribute:
- gel('lookup.assigned_to') targets the reference lookup icon for the "Assigned To" field.
- setAttribute('onclick', ...): This updates the lookup field's onclick attribute to dynamically filter users. The key part of this update is the 'sys_idIN" + userIDArr + "' clause, which modifies the reference field's query. This ensures that only the users whose IDs match the list returned by the server are displayed in the "Assigned To" lookup.
function onAssignmentGroupChange(){
var assignGroup = gel('assignment_group').value;
var ga = new GlideAjax('global.getGroupUserIDUtil');
ga.addParam('sysparm_name','getUserId');
ga.addParam('sysparm_group',assignGroup);
ga.getXML(handleXml);
}
function handleXml(response){
var userIDArr = response.responseXML.documentElement.getAttribute("answer");
var userLookUp = gel('lookup.assigned_to');
userLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'assigned_to', 'not', 'sys_user', '', 'false','QUERY:active=false','sys_idIN" + userIDArr+ "', '')");
}
Script Include Code:
-
Script Include: It runs a query on the sys_user_grmember table to get the users related to the group.
-
Returning User IDs: The function gathers the IDs of all users in the group and returns them as a comma-separated string, which is passed back to the client for use in the handleXml() function.
Note: The Script Include must be client-callable so that it can be invoked from the client-side script using GlideAjax.
var getGroupUserIDUtil = Class.create();
getGroupUserIDUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserId: function(){
var userIDArr = [];
var getGroup = this.getParameter('sysparm_group');
var getUser = new GlideRecord('sys_user_grmember');
getUser.addEncodedQuery('group='+getGroup);
getUser.query();
while(getUser.next()){
userIDArr.push(getUser.getValue('user'));
}
return userIDArr.join(',')
},
type: 'getGroupUserIDUtil'
});
Solution Overview:
- onChange Trigger: The client script is triggered whenever the "Assignment Group" is changed.
- GlideAjax Call: The onAssignmentGroupChange() function sends the selected group to the server-side Script Include using GlideAjax.
- Retrieving User IDs: The Script Include queries the sys_user_grmember table and returns the list of user IDs belonging to the selected group.
- Updating Lookup Field: The client script dynamically updates the "Assigned To" reference field, allowing only users from the selected group to be visible in the lookup.
Mark this as helpful if this helped!
Thanks,
Alka Chaudhary
- 1,397 Views