Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

On Submit validation in Catalog Item

VALLUB
Tera Contributor

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.

 

I wrote a script, it's working when user is not the member of the group correctly, throwing an error and prevents the form submission. but when user is the member of the assignment group and checks the checkbox then form is not submitted, but form should submit.

please checks the below code and correct me .

 

On submit 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';

    },
5 REPLIES 5

sharvil Sheth1
Kilo Guru

You can try by below client script

function onSubmit() {
    // Get the value of the 'name' field
    if (!g_form.ajaxComplete) {
        getData();
        return false;
    }

    function getData() {
        var ga = new GlideAjax('ScriptIncludeName');
        ga.addParam('sysparm_name', 'ScriptIncludefunctionName');
        ga.addParam('sysparm_user_id, g_form.getValue('user_id'));
        ga.getXMLAnswer(function(answer) {
            //alert(answer);
            if (answer == false || answer == 'false') {
                g_form.addErrorMessage('Please enter correct User ID');
                g_form.clearValue('user_id', true);
                return false;
            } else {
                g_form.ajaxComplete = true;
                g_form.submit();
            }
        });
    }