- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 09:41 AM
Hi everyone,
New developer here, I'm having problems creating a script to check if the current user is a member of a specific group, so that use that answer with other already existing scripts.
I'm trying to use "isMemberOf" on a Script include, then call it with a client script, without success.
I've consulted the answers on this previous post, but it doesn't to apply to my current situation.
Can anyone help me? I've just started working with GlideAjax and I'm still not sure how to pass that info, I've only used it with simpler scripts so far.
Thanks in advance for any help.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 10:21 AM
Hi
Here is the script. It's tested.
OnSubmit Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if (newValue != '' && newValue != oldValue) {
var ajax = new GlideAjax('userUtils');
ajax.addParam('sysparm_name', 'checkGroupMember');
ajax.getXML(function(answer) {
// make fields read-only here
answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
// make fields read-only here
}
});
}
}
Script Include:
var userUtils = Class.create();
userUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkGroupMember: function(){
if(gs.getUser().isMemberOf("Developer")) ////Developer is the group name
return true;
}
});
Regrads,
Snehangshu Sarkar
Please mark my answer as correct if it resolves your query.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 10:21 AM
Carlos,
Script include is not required since you are just checking who is accessing the form, you can easily do with Display Business rule and Onload script, please take sample script
Step 1: Create a display Business rule with script as.
g_scratchpad.grp = gs.getUser().isMemberOf('Name of Group');
Step 2 : Write onload script like this.
function onLoad() {
if (!g_scratchpad.grp){
g_form.setReadOnly('field_name', true);
}
}
Above script will make field read only if user is not part of group
Don't forgot to mark my answer as correct if that helps.
Regards
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 09:57 AM
Hello,
Why do you want to use ismemberof in Script include.? can you tell your requirement.?
Regards
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 10:01 AM
Hi Musab,
I've got a Client Script that applies some changes depending on the user that is accessing the ticket.
Currently I'm filtering the users by either role or sys_id, however it would be much easier to filter them by group, so I won't have to constantly update those scripts in the future.
Since isMemberOf is server side only, I figured ajax would be the correct way to access it?
Best regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 10:03 AM
Tell me whole requirement, information you provided is not sufficient
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2022 10:13 AM
Sure, thank you:
I have a client script that makes some fields "read only" depending on the user that's accessing the ticket.
Currently the script is making that decision by checking who is the user accessing the ticket, and if he has a certain role.
However that is not specific enough, instead I need to make sure the user belongs to a certain group, otherwise I will need to constantly update the script, since the many users have the same roles.
Additionally I will be able to use that Script Include in the future for other situations, so I assumed it would be the best option.
So what I've been trying to do is:
Use Script Include to check if the user is in a specific group;
Request that info with a Client Script;
Get a "True"/"False" answer, so I can do something else (displaying a simple message is fine, I'll figure out the rest myself).
Please tell me if you need any additional info.
Thanks again.
