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

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

    },
3 REPLIES 3

palanikumar
Giga Sage
Giga Sage

Hi,

You didn't assign the current user to the assigned_to field. Use the below 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.setValue("assigned_to",g_user.userID);

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

 

    }

 

    return true;
}
Thank you,
Palani

Ankur Bawiskar
Tera Patron
Tera Patron

@VALLUB 

check this link and enhance

How To: Async GlideAjax in an onSubmit script 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ChallaR
Mega Guru

Hi @VALLUB ,

 

please find the updated script -

Client Script (onSubmit):

 

var allowSubmit = false;

function onSubmit() {
    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') {
                allowSubmit = true;
                g_form.submit(); // manually trigger submit
            } else {
                g_form.addErrorMessage('You are not a member of the assignment group');
            }
        });

        return false; // prevent default submit until callback
    }

    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';
}

NOTE --

  • g_form.submit() manually triggers the form submission after the async check.
  • return false prevents the default submission until the check completes.
  • This approach works well for simple validations before form submission.

Please mark as correct it this is helpful and close the thread

 

Thanks,

Rithika.ch