Query on catalog Variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 07:55 AM
Hi All,
I have a variable on catalog form as requested_for.
How to check if this user is part of a specific group and clear a value on the other variable (select box) if not part of the group.
Thanks & Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 08:10 AM
Hi @Joshuu ,
You have to write onchangeclient script on requested for variables and send that to script include and check user member of that group and return yes or no to client script and then if it no clear a value on the other variable (select box) if not part of the group.
Onchange clinet script--
var targetGroupId = 'GROUP_SYS_ID';
var ga = new GlideAjax('GroupMembershipUtils');
ga.addParam('sysparm_name', 'isUserMemberOfGroup');
ga.addParam('sysparm_user_id', newValue);
ga.addParam('sysparm_group_id', targetGroupId);
ga.getXML(checkGroupMembership);
function checkGroupMembership(response) {
var isMember = response.responseXML.documentElement.getAttribute('answer');
if (isMember === 'false') {
g_form.setValue('other_variable_to_clear', '');
}
Script Include-
isUserMemberOfGroup: function() {
var userId = this.getParameter('sysparm_user_id');
var groupId = this.getParameter('sysparm_group_id');
var userGroups = new GlideRecord('sys_user_grmember');
userGroups.addQuery('user', userId);
userGroups.addQuery('group', groupId);
userGroups.query();
if (userGroups.hasNext()) {
return 'true';
} else {
return 'false';
}
},
Please mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 09:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 09:32 AM
Hi @Joshuu ,
You can push into an array like below
var targetGroupIds = ['GROUP_SYS_ID_1', 'GROUP_SYS_ID_2', 'GROUP_SYS_ID_3'];
Before GlideAjax use for loop
var targetGroupId= ['GROUP_SYS_ID_1', 'GROUP_SYS_ID_2', 'GROUP_SYS_ID_3'];
var ga = new GlideAjax('GroupMembershipUtils');
ga.addParam('sysparm_name', 'isUserMemberOfGroup');
ga.addParam('sysparm_user_id', newValue);
ga.addParam('sysparm_group_id', targetGroupId);
ga.getXML(checkGroupMembership);
function checkGroupMembership(response) {
var isMember = response.responseXML.documentElement.getAttribute('answer');
if (isMember === 'false') {
g_form.setValue('other_variable_to_clear', '');
}
Script include--
isUserMemberOfGroup: function() {
var parser = new JSONParser();
var userId = this.getParameter('sysparm_user_id');
var groupIds = this.getParameter('sysparm_group_id').split(',');
var groupIdpar=parser.parse(groupIds);
for (var i = 0; i < groupIdpar.length; i++) {
var userGroups = new GlideRecord('sys_user_grmember');
userGroups.addQuery('user', userId);
userGroups.addQuery('group', groupId[i]);
userGroups.query();
if (userGroups.hasNext()) {
return 'true';
} else {
return 'false';
}
},
Mark it as solution proposed and helpful if it serves you purpose.
Thanks,
Anand
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2023 10:02 AM - edited 12-04-2023 10:03 AM
Hi @Anand Kumar P ,
And here on the catalog form once they select requested for then there is a select box variable where it will have 3 choices one of the choice as admin.
So in case if user select admin then it should clear the value. I have tried to include the admin condition in client script it is not working.