Write a client script comparing current logged in user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 06:58 PM
Hie All,
I have a requirement to write a client script to achieve below scenario.
If current logged in user belongs to "Education_Dept" Group , then restrict it to update Hardware status to missing, decommissioned or NA in cmdb_ci_hardware table both form view and list view.
Here the group Education_Dept does not have user added directly to it, this group has several other groups approx 100 from where the user value is coming.
So, I have written the below script to get the value its fetching correct users from group's --> group but it not able to
compare user please suggest modifications.
var loggedInUser = g_user.userID;
var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('parent','Education_Dept');
groupGR.query();
while(groupGR.next()){
var groupId = groupGR.sys_id.toString();
var groupMemberGR = new GlideRecord('sys_user_grmember');
groupMemberGR.addQuery('group',groupId);
groupMemberGR.query();
while(groupMemberGR.next()){
var groupMember = groupMemberGR.user.toString();
if(groupMember === loggedInUser){
// then should not be able to change hardware status to missing or decommed or NA
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 07:33 PM
@Ankita NA
How about using below code.. and it only works on server side .
You should move the check logic to business rule or script include ( call it via glideajax).
var currentUser = gs.getUser();
gs.info(currentUser.isMemberOf('Capacity Mgmt'));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 07:42 PM
Hi @Ankita NA ,
The best approach for this would be to go with GlideAjax n script include as it is not recommended to use GlideRecord in client script.
Mentioning few articles with different approach for these.
https://www.trylearngrowrepeat.com/check-if-member-is-already-in-group/
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 07:44 PM
Hi @Ankita NA
It is not good practice to use GlideRecord at client side, instead create a client callable script include and call it in your client script. Try the following code,
1. CLient Callable Script Include:
var CheckEducationDeptUser = Class.create();
CheckEducationDeptUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkLoggedInUser: function(){
var gr = new GlideRecord("sys_user_grmember");
gr.addEncodedQuery("group.parent.name=Education_Dept^user.sys_id" + gs.getUserID());
gr.query();
if(gr.hasNext()){
return "true";
}
return "false";
},
type: 'CheckEducationDeptUser'
});
2. Client Script:
Use the following client script snippet in any of your client script like onLoad, onChange or onCellEdit. Just add the logic to restrict user.
var ga = new GlideAjax("CheckEducationDeptUser");
ga.addParam("sysparm_name", "checkLoggedInUser");
ga.getXMLAnswer(checkUser);
function checkUser(answer){
if(answer == 'true'){
//Restrict the user from editing the field you want
}
}
Please mark my answer helpful and accept as solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2023 07:46 PM
why not handle it via field level WRITE ACL?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader