Use isMemberOf with Ajax

Carlos52
Tera Expert

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.

2 ACCEPTED SOLUTIONS

Snehangshu Sark
Mega Guru

Hi @Carlos @Carlos @Carlos    

 

 

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.

View solution in original post

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

Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

10 REPLIES 10

Musab Rasheed
Tera Sage

Hello,

Why do you want to use ismemberof in Script include.? can you tell your requirement.?

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

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

Tell me whole requirement, information you provided is not sufficient

Please hit like and mark my response as correct if that helps
Regards,
Musab

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.