onload client script to check if the logged in user is par of which all groups
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 11:21 AM - edited 02-15-2024 11:24 AM
Hi Experts
My requirement is I need to hide few fields in the scoped application form extended on task table.
the form is only accessible for the below group
group1 'xyz'
group2 'efg'
group3 'mnp'
group4'abc'
the form has three sections top section middle section and lower section
the top section should be accessible for only for group1
if the user is only part of group1 then all other sections should be readonly.
like wise the middle section should be accessible for only group2
if the user is only part of group2 then all other sections should be readonly
same with group3 and group4
but if the user is part of all the group or part of two or three groups then relevant sections should be accessible
for example if user is part of group1 and group2 then the user should acccess both top and middle vice versa
I tried with Glide Ajax in client script by calling the groups from script include
but it not working correctly.
the script include is not check for all the groups the user belongs for. it comes out of the loop if any of the group it matches.
My script include
My client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2024 11:27 AM
I'd try creating a new view and a view rule. In your view rule you can use your script to check which group and then if a member, force that view. Lastly, you can apply UI Policies / Client Scripts at the view level, so you can make your fields read only for that view specifically if that is your goal.
Good luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2024 09:33 PM
Hi Guru,
Thank you for the response. I did try exploring the view rules but unfortunately it will only allow to hide the fields not make read only. So this option is not working for my issue. I did try to call the array from the script include but not sure how to check the array response from the client script. I did try calling response from script include by using the operators contains or includes but nothing is working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 01:36 AM - edited 02-18-2024 01:36 AM
Hi @Community Alums
It seems like the issue with your script is that it's returning from the function as soon as it finds a match in the loop. This behavior causes it to only check the first group and then exit the loop. To address this, you can modify your script to check all groups and then decide the appropriate action based on the user's membership in each group.
Here's the modified script include code:
var CheckuserGroups = Class.create();
CheckuserGroups.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getGroups: function() {
var usersysid = this.getParameter('sysparm_name_sysid');
var groupsToCheck = ['6f0596dadbe8901004882b24ca9619ad', 'fe17dfb41b3d25509ebb64a2b24bcb08', '5a16e9331b3cf8d0e3cadd77b04bcb0a', 'c52f8ac5db0d9410b9e09334ca96195e'];
var userGroups = [];
for (var i = 0; i < groupsToCheck.length; i++) {
var mem = new GlideRecord("sys_user_grmember");
mem.addQuery('user', usersysid);
mem.addQuery('group', groupsToCheck[i]);
mem.query();
while (mem.next()) {
userGroups.push(mem.getValue('group'));
}
}
return userGroups;
},
type: 'CheckuserGroups'
});
Now, the getGroups function will return an array containing all the groups the user belongs to. With this modification, you can adapt your client script accordingly to handle all scenarios. Here's an updated version of your client script:
function onLoad() {
var sysid = g_user.userID;
var ga = new GlideAjax('CheckuserGroups');
ga.addParam('sysparm_name', 'getGroups');
ga.addParam('sysparm_name_sysid', sysid);
ga.getXMLAnswer(getUserGroups);
function getUserGroups(response) {
var userGroups = response.split(',');
var currentState = g_form.getValue('state');
if (userGroups.includes('fe17dfb41b3d25509ebb64a2b24bcb08')) {
// Group1 - xyz
if (currentState == '610' || currentState == '620' || currentState == '630') {
g_form.setReadOnly('u_escalation_response', true);
g_form.setReadOnly('u_response_reason', true);
g_form.setReadOnly('u_resp_ext_reason', true);
}
}
if (userGroups.includes('6f0596dadbe8901004882b24ca9619ad')) {
// Group2 - efg
if (currentState == '610') {
g_form.setReadOnly('u_response_reason', false);
g_form.setReadOnly('u_resp_ext_reason', false);
g_form.setReadOnly('u_escalation_response', false);
}
if (currentState == '620' || currentState == '630') {
g_form.setReadOnly('u_manager_review', false);
g_form.setReadOnly('u_review_reason', false);
}
}
if (userGroups.includes('5a16e9331b3cf8d0e3cadd77b04bcb0a')) {
// Group3 - mnp
if (currentState == '610' || currentState == '620' || currentState == '630') {
g_form.setReadOnly('u_response_reason', false);
g_form.setReadOnly('u_resp_ext_reason', false);
g_form.setReadOnly('u_escalation_response', false);
}
}
if (userGroups.includes('c52f8ac5db0d9410b9e09334ca96195e')) {
// Group4 - abc
if (currentState == '610') {
g_form.setReadOnly('u_response_reason', true);
g_form.setReadOnly('u_resp_ext_reason', true);
g_form.setReadOnly('u_escalation_response', true);
}
if (currentState == '620' || currentState == '630') {
g_form.setReadOnly('u_manager_review', false);
g_form.setReadOnly('u_review_reason', false);
}
}
}
}
Please mark as helpful & accepted solution if it suffice
BR, Sohith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2024 09:16 PM
Hi Sohithanjan G
Thank you for sharing the script. not sure but the includes method is not working in the client script at my end.
The below script is working at my end
MyScriptInclude:
// Set the prototype on the global scope
//global.MyScriptInclude = Class.create();
//MyScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
var MyScriptInclude = Class.create();
MyScriptInclude.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkUserGroups: function() {
var userId = this.getParameter('sysparm_user_id');
var group1 = ['fddfdfdsfdsfdsfdsfdsfdsfdsfdsfsf'];
var group2 = ['fsdfdsedsfefsdfwesdfefefwefefeww'];
var group3 = ['sdgdsfdsffsdfsdfsdfsfsdfsdfsdfff'];
var group4 = ['sdsefsefweweewwefewrwerweewewrew'];
var resultGroup1 = this.isUserInGroupArray(userId, group1);
var resultGroup2 = this.isUserInGroupArray(userId, group2);
var resultGroup3 = this.isUserInGroupArray(userId, group3);
var resultGroup4 = this.isUserInGroupArray(userId, group4);
var result = '';
if (resultGroup1) {
result += 'Group1';
}
if (resultGroup2) {
result += 'Group2';
}
if (resultGroup3) {
result += 'Group3';
}
if (resultGroup4) {
result += 'Group4';
}
gs.info('Debug: ResultGroup1: ' + resultGroup1);
gs.info('Debug: ResultGroup2: ' + resultGroup2);
gs.info('Debug: ResultGroup3: ' + resultGroup3);
gs.info('Debug: ResultGroup4: ' + resultGroup4);
return result;
},
isUserInGroupArray: function(userId, groupArray) {
gs.info('Debug: Checking user ' + userId + ' in groups ' + JSON.stringify(groupArray));
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', userId);
grMember.addQuery('group', 'IN', groupArray);
grMember.query();
gs.info('Debug: Query executed. Records found: ' + grMember.getRowCount());
return grMember.hasNext();
}
});
Clientscriptnew:
// OnLoad Client Script
function onLoad() {
var userId = g_user.userID;
// Call the server script include using GlideAjax
var ga = new GlideAjax('MyScriptInclude'); // Replace 'MyScriptInclude' with the actual name of your script include
ga.addParam('sysparm_name', 'checkUserGroups');
ga.addParam('sysparm_user_id', userId);
ga.getXMLAnswer(onSuccess); // Assuming the server script returns a value
function onSuccess(response) {
//alert('User Groups: ' + response);
if ((response == 'Group1') && (g_form.getValue('state') == '610')
||(g_form.getValue('state') == '620')||(g_form.getValue('state') == '630')) {
g_form.setReadOnly('u_escalation_response', true);
g_form.setReadOnly('u_response_reason', true);
g_form.setReadOnly('u_resp_ext_reason', true);
g_form.setReadOnly('u_manager_review',true);
g_form.setReadOnly('u_review_reason',true);
}