Not allow form to submit

kaushiki berter
Tera Expert

My requirement is to not allow users to submit the record producer form if already same short description and description has been entered in previous requests. I have written on submit client script where i have made a glideajax call in order to receive data from server which is checking whether the incident is having same combination of short description and description so everything is working fine but I am not able to execute return false once conditions match from the existing data and hence in all cases the form is getting submitted. 

kaushikiberter_0-1732550955177.png

client script -

function onSubmit() {
    //Type appropriate comment here, and begin script below

    var shortDescription = g_form.getValue('short_description');
    var description = g_form.getValue('description');

    var flag; // Default to false to prevent submission

    alert(shortDescription + " " + description);

    var ga = new GlideAjax('inci');
    ga.addParam('sysparm_name', 'fetch');
    ga.addParam('sysparm_val', shortDescription);
    ga.addParam('sysparm_valu', description);
    if ((!shortDescription) && (!description)) {
        return false;
    }
    ga.getXMLAnswer(function(answer) {
        alert(answer);
        if (answer === 'true') {
            flag = "yes";
            alert(flag);

            return false;

            //return false;
        } else {
            flag = "no";
        }

    });  
Script Include
fetch: function() {
        var o = this.getParameter('sysparm_val');
        var k = this.getParameter('sysparm_valu');
        gs.log('o' + o);
        gs.log('inxxxx' + k);

        var gr = new GlideRecord('incident');
        gr.addQuery('short_description', o);
        gr.addQuery('description', k);
        gr.query();
        if (gr.next()) {
            kt = 'true';
        } else {
            kt = 'false';
        }
        gs.log("true" + kt);
        return kt;


    },
If there is any simpler approach, then kindly let me know else please help me with above client script.
11 REPLIES 11

Thank you for the solution, Juhi. Unfortunately return false is not working here as well.

Tai Vu
Kilo Patron
Kilo Patron

Hi @kaushiki berter 

You can give my adjusted client script a try.

function onSubmit() {

    /*** Pop this gem into your script! */
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var shortDescription = g_form.getValue('short_description');
    var description = g_form.getValue('description');
    if ((!shortDescription) && (!description)) { //you can just make these variables mandatory
        return false;
    }

    var ga = new GlideAjax('inci');
    ga.addParam('sysparm_name', 'fetch');
    ga.addParam('sysparm_val', shortDescription);
    ga.addParam('sysparm_valu', description);
    ga.getXMLAnswer(function(answer) {
        if (answer === 'true') {
            return false;
        } else {
            /*** Pop this gem into your script! */
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    });
	
	/*** Pop this gem into your script! */
	return false;
}

 

Reference:

Onsubmit catalog client script is not aborting the form from submitting

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

Timi_0-1732605370438.png

 

Cheers,

Tai Vu