- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 10:58 PM
We have one field "user" and another field "group".
When we add the user in the reference field "user" then if user is member of group "RSD" then RSD gorup should be auto populated to the field "group".
User can be mmember of multiple group but here we are only concern about the RSD group.
We want to achive this thorugfh on change client script like if we add the user name in user field then RSD group should auto populate to the field group.
Please suggest the onchange client script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 11:04 PM
you can use onChange client script with GlideRecord callback
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading || newValue == '') {
return;
}
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", newValue);
gr.addQuery("group.name", "RSD"); // give the group name
gr.setLimit(1);
gr.query(checkRecord);
function checkRecord(gr) {
if (gr.next()) {
g_form.setValue('group', gr.group);
} else {
g_form.clearValue('group');
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 11:04 PM
you can use onChange client script with GlideRecord callback
function onChange(control, oldValue, newValue, isLoading) {
if (!isLoading || newValue == '') {
return;
}
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("user", newValue);
gr.addQuery("group.name", "RSD"); // give the group name
gr.setLimit(1);
gr.query(checkRecord);
function checkRecord(gr) {
if (gr.next()) {
g_form.setValue('group', gr.group);
} else {
g_form.clearValue('group');
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2025 11:09 PM
Hello,
You can do something like below, take this as a reference and modify according to your needs :
Client Script Details:
Type: onChange
Field: user
Table: Applicable table where this functionality is required
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Perform a GlideAjax call to check if the user is in the RSD group
var ga = new GlideAjax('CheckRSDGroup');
ga.addParam('sysparm_name', 'isUserInRSDGroup');
ga.addParam('sysparm_user_id', newValue); // sys_id of the selected user
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.setValue('group', 'sys_id_of_RSD_group'); // Replace 'sys_id_of_RSD_group' with the actual sys_id of the RSD group
} else {
g_form.clearValue('group'); // Clear the group field if the user is not in the RSD group
}
});
}
Script Include:
var CheckRSDGroup = Class.create();
CheckRSDGroup.prototype = {
initialize: function() {},
isUserInRSDGroup: function() {
var userId = this.getParameter('sysparm_user_id');
var groupName = 'RSD';
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('user', userId);
userGR.addQuery('group.name', groupName);
userGR.query();
return userGR.hasNext() ? 'true' : 'false';
},
type: 'CheckRSDGroup'
};
I hope this information helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2025 12:50 AM - edited 01-24-2025 12:50 AM
Hello @VIKAS MISHRA ,
You need an additional script include to achieve what you need where you can implement it in the onChange Client script.
For your script include :
var UserGroupMembershipCheck = Class.create();
UserGroupMembershipCheck.prototype = {
initialize: function() {
},
isUserInRSDGroup: function() {
var userSysId = this.getParameter('sysparm_user_sys_id');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userSysId);
gr.addQuery('group.name', 'RSD');
gr.query();
return gr.hasNext();
},
type: 'UserGroupMembershipCheck'
};
Then for your OnChange Client script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('UserGroupMembershipCheck');
ga.addParam('sysparm_name', 'isUserInRSDGroup');
ga.addParam('sysparm_user_sys_id', newValue);
ga.getXML(checkMembership);
function checkMembership(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer === 'true') {
var gr = new GlideRecord('sys_user_group');
gr.addQuery('name', 'RSD');
gr.query();
if (gr.next()) {
g_form.setValue('group', gr.sys_id);
}
}
}
}
If this has been of any help, click the thumbs up or mark as solutions for others to reference to and might be of help.
Thanks!