form submission with glid ajax

Pavan rishi
Tera Contributor
function onSubmit() {
    // Get the Clarity ID from the form
    var clarity_id = g_form.getValue('id');

    // Create a new GlideAjax instance
    var ga = new GlideAjax('Cvks');
    ga.addParam('sysparm_name', 'validateValue');
    ga.addParam('sysparm_value', id);

    // Call getXML with the callback function
    var clar = ga.getXML(value);

    // Callback function to handle the response
    function value(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == "IncorrectID") {
            g_form.addErrorMessage("incorrect"); / 

        } else if (answer == "CorrectID") {
          g_form.addErrorMessage('correct');
  
        }
    }
}
 
when answer is incorrectId I don't want to submit form? How can i do that?
1 ACCEPTED SOLUTION

@Pavan rishi 

Let's give my adjustment below for your given script.

function onSubmit() {

    //Add below lines to your script
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    // Get the Clarity ID from the form
    var clarity_id = g_form.getValue('id');

    // Create a new GlideAjax instance
    var ga = new GlideAjax('Cvks');
    ga.addParam('sysparm_name', 'validateValue');
    ga.addParam('sysparm_value', id);

    // Call getXML with the callback function
    var clar = ga.getXML(value);

    //Add this to your script
    return false;

    // Callback function to handle the response
    function value(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == "IncorrectID") {
            g_form.addErrorMessage("incorrect");

            //Add this line to your script.
            return false;
        } else if (answer == "CorrectID") {
            g_form.addErrorMessage('correct');

            //Add below lines to your script.
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    }

}

 

Cheers,

Tai Vu

View solution in original post

2 REPLIES 2

Tai Vu
Kilo Patron
Kilo Patron

Hi @Pavan rishi 

Let's have a look to the below article on how to do async validation in an OnSubmit client script.

The GlideAjax (Asynchronous) does not work on onSubmit Client Script. This is because of the fundamental behavior of Asynchronous scripts which are non-blocking by nature.

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

Timi_0-1730263595980.png

 

Cheers,

Tai Vu

@Pavan rishi 

Let's give my adjustment below for your given script.

function onSubmit() {

    //Add below lines to your script
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    // Get the Clarity ID from the form
    var clarity_id = g_form.getValue('id');

    // Create a new GlideAjax instance
    var ga = new GlideAjax('Cvks');
    ga.addParam('sysparm_name', 'validateValue');
    ga.addParam('sysparm_value', id);

    // Call getXML with the callback function
    var clar = ga.getXML(value);

    //Add this to your script
    return false;

    // Callback function to handle the response
    function value(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");

        if (answer == "IncorrectID") {
            g_form.addErrorMessage("incorrect");

            //Add this line to your script.
            return false;
        } else if (answer == "CorrectID") {
            g_form.addErrorMessage('correct');

            //Add below lines to your script.
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        }
    }

}

 

Cheers,

Tai Vu