How to restrict form submission if user is not a part of a group in catalog item?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 08:54 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:25 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:42 PM
Hi Tadz,
Its not working. I have written one client script and script include but its not restricting 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'
});
Can you please help me with the restriction?
Thanks in advance

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:47 PM
Hi, have you tried this script yet?
This would work if your Prod team has a unique role i find this approach easier.
Else you need to use isMemberOf function which is a server side script.
see reference:
https://community.servicenow.com/community?id=community_question&sys_id=18e01ba9dbdcdbc01dcaf3231f9619cd
Let me know it this helps 🙂

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2022 09:50 PM
Is your glideajax returning results property?
If yes, possible reason is your glideajax is async and it didn't wait for the result hence it proceeds on submission.
You could try to make your glideajax synchronous.
But i would prefer my first approach (using g_user.hasRole) as it is much simpler.