Onsubmit catalog client script is not aborting the form from submitting.

Kruthik M Shiva
Tera Contributor

Hi All,
I have created a on submit client script if the condition is matched it should give alert message and abort action i have used return false but still form is getting submitted what could be the possible issue and how to fix it, please share if anyone has pointers on this.
Thanks in advance.
Kruthik Shivaprasad

1 ACCEPTED SOLUTION

Hi @Kruthik M Shiva 

Add one more line "return false" to the bottom of the client script. It should work

function onSubmit() {

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

    var ga = new GlideAjax('x_intellectu.CheckRetired');
    ga.addParam('sysparm_name', 'CheckingRetired');
    ga.addParam('sysparm_ia_name', g_form.getValue('ia_name'));
    ga.getXML(RetiredCheck);

    function RetiredCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            alert('This is retired');
            return false;
        }
        /*** Pop this gem into your script! */
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
	/*** Pop this gem into your script! */
	return false;
}

 

Cheers,

Tai Vu

View solution in original post

12 REPLIES 12

Jaspal Singh
Mega Patron
Mega Patron

Hi Kruthik,

Can you share the script once for a check.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Kruthik M Shiva 

What is the condition that you've built in your script? Is it called a GlideAjax?

If yes, the form is still submitted because of the Asynchronous GlideAjax call.

Check the below approach

TaiVu_0-1697039591607.png

HI @Tai Vu ,
So based on my code what changes need to be done in my code can you please help me on this.
Thanks

Hi @Kruthik M Shiva 

Let's try this

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

    var ga = new GlideAjax('x_intellectu.CheckRetired');
    ga.addParam('sysparm_name', 'CheckingRetired');
    ga.addParam('sysparm_ia_name', g_form.getValue('ia_name'));
    ga.getXML(RetiredCheck);

    function RetiredCheck(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer == 'true') {
            alert('This is retired');
            return false;
        }
        /*** Pop this gem into your script! */
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }

 

Let me know if it works for you!

 

Cheers,

Tai Vu