On submit validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi Community,
I have one requirement, In form level having requestor for filed and assignment group filed, requestor field taking the user table and in assignment group filed taking the group table, and in my requirement they ask me to create one new check box filed which is assign to me and if user check the check box which is assign to me and user is the member of that group then the form should submits and if user is not member of that group then form should not be submitted.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago - last edited a month ago
Hi @VALLUB ,
You can achieve this requirement by using a hidden checkbox and two client scripts to control the form submission. Here's a simple solution:
Create a Hidden Field
- Name: valid_submit (Type: Checkbox)
- This field will help control when the form is allowed to submit.
OnSubmit Client Script
- Name: Validate Requestor
- This script checks if the "Assign to Me" checkbox is selected. If yes, it verifies if the requestor is part of the selected assignment group.
- If the check passes, it sets valid_submit to true.
function onSubmit() {
var validSubmit = g_form.getValue('valid_submit');
if (validSubmit == 'false') {
var assignToMe = g_form.getValue('assign_to_me') == 'true';
var assignmentGroup = g_form.getValue('assignment_group');
var requestor = g_form.getValue('requestor');
if (assignToMe) {
var ga = new GlideAjax('UserGroupMembership');
ga.addParam('sysparm_name', 'isUserMemberOfGroup');
ga.addParam('sysparm_group_id', assignmentGroup);
ga.addParam('sysparm_user_id', requestor);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
g_form.setValue("valid_submit", true);
} else {
g_form.addErrorMessage("Requestor is not a member of the selected group.");
}
});
}
return false;
}
}
OnChange Client Script
- Name: Form Submission on Success
- Field: valid_submit
- This script automatically submits the form once the validation is successful.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue == true) {
g_form.submit();
}
}
Script Include
- Name: "UserGroupMembership"
- Glide AJAX enabled: true
- Validates whether a user is a member of a specific group. Used by the OnSubmit script.
var UserGroupMembership = Class.create();
UserGroupMembership.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isUserMemberOfGroup: function() {
var userId = this.getParameter('sysparm_user_id');
var groupId = this.getParameter('sysparm_group_id');
if (!userId || !groupId) return 'false';
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userId);
gr.addQuery('group', groupId);
gr.setLimit(1);
gr.query();
return gr.hasNext() ? 'true' : 'false';
},
type: 'UserGroupMembership'
});
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
