How to restrict form submission if user is not a part of a group in catalog item?

User205031
Tera Contributor

Hi All,

I have a requirement. I have two fields 'Requested for' and 'Product Group'. I need to check if 'Requested for' user is part of the 'Product Group'. If he is a part of the group the form will get submitted or else it will throw an error message and the form won't get submitted.

I have written one client script and script include but its not restricting the users to submit.

Client Script:

function onSubmit() {
//Type appropriate comment here, and begin script below

var userName = g_form.getValue('requested_for');
alert(userName);
var prodGrp = g_form.getValue('product_team');
alert(prodGrp);

var ga = new GlideAjax('RestrictUser');
ga.addParam("sysparm_name", "checkUser");
ga.addParam("sysparm_user_name", userName);
ga.addParam("sysparm_grp_name", prodGrp);
ga.getXML(checkProdTeam);


function checkProdTeam(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

alert(answer);
var stop = false;

if (answer == 'false') {
stop = true;
//g_form.setValue('product_team', answer);
g_form.addErrorMessage(getMessage('User not part of Product Team'));
return false;
}
}

}

 

Script Include:

var RestrictUser = Class.create();
RestrictUser.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkUser: function(){

var uname = this.getParameter('sysparm_user_name');
var pgroup = this.getParameter('sysparm_grp_name');

var a = new GlideRecord('sys_user_grmember');
a.addQuery('user', uname);
a.addQuery('group', pgroup);
a.query();
if(a.next()){
return true;
}
else {
return false;
}

},


type: 'RestrictUser'
});

Please help!

Thanks in advance!

10 REPLIES 10

User205031
Tera Contributor

Hi Tadz,

We don't have any role in the Product team hence won't be able to use the first approach. Can you help me with the client script and script include?

Thanks

Your script seems correct.

can you try checking this link if this could help


https://community.servicenow.com/community?id=community_question&sys_id=e47ff36fdb449c10f7fca851ca9619be

 

User205031
Tera Contributor

Hi @Ankur Bawiskar  can you help me with this?

Thanks!

Prashant16
Kilo Guru

Hi,

Instead of doing this you can get only the members of Product group in Requested for by adding 1 line code. Let me know if you want to implement this.

 

Thanks,

Prashant

Hi Prashant,

I want to check if the requested for user is part of the product group, then only he will be able to submit the form otherwise not.

Thanks