- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 02:28 PM
Hello,
I am tasked with configuring an Ajax call on an on submit client script to check if a user is in a group, and if he isn't found in the group, post an error message. I've never worked with Ajax calls before and am unsure of how to proceed. Thank you.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 06:20 PM
Hai
According to Scenario when submit form before validate the user is member a particular group.
whenever we are using 'GlideAjax' in our client scripts means in our client script is checking server side validation.
pls check examples GlideAjax...
According to your requirement the below code you can use
In From i have one reference flied name as 'caller' and checking always my caller is member of 'code team development' group if not a member of that group throw a error --->This scenario is replicate your requirement
create a client script :
function onSubmit() {
// getting caller flied value.
var callerID = g_form.getValue('u_caller');
//pass this valu to sever side and check weather this caller is member of 'code team developmet'
var ga = new GlideAjax('CallerGroupValidation');
ga.addParam('sysparm_name', 'validateCaller');
ga.addParam('sysparm_callerid', callerID);
ga.getXML(updateCampus);
}
function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.addInfoMessage('caller is member of code team developmet group');
} else {
g_form.addErrorMessage('caller is not member of code team developmet group');
return false;
}
}
Script include:
var CallerGroupValidation = Class.create();
CallerGroupValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateCaller: function(){
var callerID = this.getParameter('sysparm_callerid');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', callerID);
gr.query();
if(gr.next()){
return true;
} else {
return false;
}
},
type: 'CallerGroupValidation'
});
Please Mark ✅ Correct/helpful, if applicable, Thanks!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 05:13 PM
1. Created fields "user" and "group" referencing tables "sys_user" and "sys_user_group" respectively.
2. Create onChange client script on variable name:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || oldValue == newValue) {
return;
}
var group_id = g_form.getValue('group');
if (group_id == '') {
return;
}
var ajax = new GlideAjax('UserInfo4');
ajax.addParam('sysparm_name', 'isUserInGroup');
ajax.addParam('sysparm_user_id', newValue);
ajax.addParam('sysparam_group_id', group_id);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.showFieldMsg('user', answer);
}
});
}
3. Create onChange client script on variable group:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '' || oldValue == newValue) {
return;
}
var user_id = g_form.getValue('user');
if (user_id == '') {
return;
}
var ajax = new GlideAjax('UserInfo4');
ajax.addParam('sysparm_name', 'isUserInGroup');
ajax.addParam('sysparm_user_id', user_id);
ajax.addParam('sysparam_group_id', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.showFieldMsg('group', answer);
}
});
}
4. Create script include:
var UserInfo4 = Class.create();
UserInfo4.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserInGroup: function() {
var user_id = this.getParameter('sysparm_user_id');
var group_id = this.getParameter('sysparam_group_id');
var grUser = new GlideRecord('sys_user_grmember');
grUser.addQuery('user', user_id);
grUser.addQuery('group', group_id);
grUser.query();
if (grUser.next()) {
return "true";
}
return 'false';
},
type: 'UserInfo4'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 05:15 PM
For information on what the script is doing, refer to the following article.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2022 06:20 PM
Hai
According to Scenario when submit form before validate the user is member a particular group.
whenever we are using 'GlideAjax' in our client scripts means in our client script is checking server side validation.
pls check examples GlideAjax...
According to your requirement the below code you can use
In From i have one reference flied name as 'caller' and checking always my caller is member of 'code team development' group if not a member of that group throw a error --->This scenario is replicate your requirement
create a client script :
function onSubmit() {
// getting caller flied value.
var callerID = g_form.getValue('u_caller');
//pass this valu to sever side and check weather this caller is member of 'code team developmet'
var ga = new GlideAjax('CallerGroupValidation');
ga.addParam('sysparm_name', 'validateCaller');
ga.addParam('sysparm_callerid', callerID);
ga.getXML(updateCampus);
}
function updateCampus(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.addInfoMessage('caller is member of code team developmet group');
} else {
g_form.addErrorMessage('caller is not member of code team developmet group');
return false;
}
}
Script include:
var CallerGroupValidation = Class.create();
CallerGroupValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateCaller: function(){
var callerID = this.getParameter('sysparm_callerid');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', callerID);
gr.query();
if(gr.next()){
return true;
} else {
return false;
}
},
type: 'CallerGroupValidation'
});
Please Mark ✅ Correct/helpful, if applicable, Thanks!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 08:00 AM
Hi Sekhar,
This is very helpful, thank you very much! Quick question, where in your code is the check to see if the user is a member of the Code Team group? I can't seem to find the check you implemented.