The CreatorCon Call for Content is officially open! Get started here.

On submit validations

VALLUB
Tera Contributor
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.
 
I wrote the below Onsubmit client script and script Include, but when user is member of the assignment group also form is not submitting, when user is not member of the group then it working correctly, but when user is part of the assignment then form should submits.
 
Client Script:
 
function onSubmit() {
    //Type appropriate comment here, and begin script below

 

    var assignmentGroup = g_form.getValue('assignment_group');
    var user = g_form.getValue('requested_for');
    var assignToMe = g_form.getValue('assign_to_me');

 

    if (assignToMe === 'true') {
        var ga = new GlideAjax('GetUserDetails');
        ga.addParam('sysparm_name', 'isUserInGroup');
        ga.addParam('sysparm_user_id', user);
        ga.addParam('sysparm_group_id', assignmentGroup);
        ga.getXMLAnswer(function(response) {
            if (response === 'true') {
                g_form.submit();

 

            } else {
                g_form.addErrorMessage('you are not a member of the assignment group');
            }
        });

 

        return false;
    }

 

    return true;
}
}
 
Script Include:
 
isUserInGroup: function() {
        var userId = this.getParameter('sysparm_user_id');
        var groupId = this.getParameter('sysparm_group_id');

        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group', groupId);
        gr.query();

        if (gr.hasNext()) {
            return 'true';
        }
        return 'false';

    },
 
And in my ServiceNow portal only Request button is there, no submit button available, please check the below code and help me accordingly, Thanks in Advance.
2 REPLIES 2

Bert_c1
Kilo Patron

Try:    if (response == 'true') {

in you client script, as '===' is more restrictive.

Brad Bowman
Kilo Patron
Kilo Patron

As with any script debugging, add alerts to your client script and gs.info lines to your script include to see how far it is getting, which if conditions are being met, and the value(s) passed from one to the other.  Is your script include client callable and does it look like this at the beginning?

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

In the client script, if the response is true, return true, don't submit as the submit action is already in progress when this script is running.  You can also simplify your script include like this:

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

    isUserInGroup: function() {
        var userId = this.getParameter('sysparm_user_id');
        var groupId = this.getParameter('sysparm_group_id');
        var thisUser = gs.getUser().getUserByID(userId);
        return thisUser.isMemberOf(group);
    },
    type: 'GetUserDetails'
});