MRVS Catalog Client Script

SO10
Tera Contributor

Dear Community,

I am encountering an issue with the g_form.submit() function in a ServiceNow catalog item. Here is the scenario:

I have a Multi Row Variable Set (MRVS) in the catalog item, which includes a variable for entering User ID. Upon clicking the "Add" button, the script should check for duplicate records in the sys_user table based on the values entered in the user_id variable. If a duplicate record is found, an error message should be displayed in a popup window in the MRVS, and the process should be aborted. If no duplicate record is found, the process should continue.

I have implemented the following script include and catalog client script for this functionality:

Script Include:

 

 

checkDuplicateUserID: function() {
    var userID = this.getParameter('sysparam_userid');
    var gr = new GlideRecord('sys_user');
    gr.addQuery('user_name', userID);
    gr.query();
    if (gr.hasNext()) {
        return gs.getMessage('Duplicate record found');
    }
    return '';
},

 

 

Catalog Client Script:

 

 

function onSubmit() {
    var userID = g_form.getValue('user_id');

    var ga = new GlideAjax('global.testscriptinc');
    ga.addParam('sysparm_name', 'checkDuplicateUserID');
    ga.addParam('sysparam_userid', userID);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            g_form.addErrorMessage(answer);
        } else {
            g_form.submit();
        }
    });

    return false;
}

 

 

The issue I am facing is that the g_form.submit() function does not seem to work as expected. Even when there is no duplicate record found and the else block is executed, the form does not submit and the MRVS popup remains open without adding a new row.

Could you please provide guidance on how to resolve this issue? Thank you in advance for your help.

Best regards,

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @SO10 

It's because you're performing submit action g_form.submit() and missing return true. Then when the form get resubmitted, it again comes to the else and resubmit it, results in a loop.

Let's have a look to how to do Asynchronous GlideAjax call on submit client script.

Timi_0-1709625374506.png

 

Your script should be liked below

 

 

 

function onSubmit() {
	//add this condition
	if (g_scratchpad.isFormValid){
		return true;
	}

    var userID = g_form.getValue('user_id');
    var ga = new GlideAjax('global.testscriptinc');
    ga.addParam('sysparm_name', 'checkDuplicateUserID');
    ga.addParam('sysparam_userid', userID);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            g_form.addErrorMessage(answer);
        } else {
			g_scratchpad.isFormValid = true; //add this line
            g_form.submit();
        }
    });
    return false;
}

 

 

 

 

Ref: KB0783579 - How to do async validation in an onsubmit client script.

 

Cheers,

Tai Vu

View solution in original post

2 REPLIES 2

Tai Vu
Kilo Patron
Kilo Patron

Hi @SO10 

It's because you're performing submit action g_form.submit() and missing return true. Then when the form get resubmitted, it again comes to the else and resubmit it, results in a loop.

Let's have a look to how to do Asynchronous GlideAjax call on submit client script.

Timi_0-1709625374506.png

 

Your script should be liked below

 

 

 

function onSubmit() {
	//add this condition
	if (g_scratchpad.isFormValid){
		return true;
	}

    var userID = g_form.getValue('user_id');
    var ga = new GlideAjax('global.testscriptinc');
    ga.addParam('sysparm_name', 'checkDuplicateUserID');
    ga.addParam('sysparam_userid', userID);
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            g_form.addErrorMessage(answer);
        } else {
			g_scratchpad.isFormValid = true; //add this line
            g_form.submit();
        }
    });
    return false;
}

 

 

 

 

Ref: KB0783579 - How to do async validation in an onsubmit client script.

 

Cheers,

Tai Vu

Amit Verma
Kilo Patron
Kilo Patron

Hi @SO10 

 

I can see a small typo in your Script Include function and in Client Script (sysparam instead of sysparm). Also, in the Client Script, in the else block, you have not allowed the form to be submitted. Please try with below Script Include function and Client Script :

 

checkDuplicateUserID: function() {
    var userID = this.getParameter('sysparm_userid'); // Changed sysparam to sysparm
    var gr = new GlideRecord('sys_user');
    gr.addQuery('user_name', userID);
    gr.query();
    if (gr.hasNext()) {
        return gs.getMessage('Duplicate record found');
    }
    return '';
},

 

function onSubmit() {
    var userID = g_form.getValue('user_id');
    var ga = new GlideAjax('global.testscriptinc');
    ga.addParam('sysparm_name', 'checkDuplicateUserID');
    ga.addParam('sysparm_userid', userID); // Replaced sysparam with sysparm
    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer) {
            g_form.addErrorMessage(answer);
        } else {
            return true; // Allow the form to be submitted
        }
    });

    return false;
}

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.