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

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.