Return false not working in on submit client script

Sajal Chatterje
Tera Contributor

Hello Community,

 

There is a requirement where if a catalog form passes all the validations then only the form should be submitted otherwise we have to abort the on submit. It should not submit anything.

 

I used return false in my client script but still user able to submit the form. Please let me know for any other alternative to abort on submit action. Below is my script i used.

 

Client script
function onSubmit() {
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('CheckDuplicateDelegate');
    ga.addParam('sysparm_name', 'chkDuplicate'); // the name has to be changed
    ga.addParam('sysparm_user', g_form.getValue('requested_by'));
    ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));

    ga.getXML(checkDuplicateDelegate);

    function checkDuplicateDelegate(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
       

        if (answer == "true"){
        g_form.addErrorMessage("There is already an existing delegation for this user and delegate with the same delegated items within the same time period.");
        return false;
}

 

 

 

Script Include

var CheckDuplicateDelegate = Class.create();
CheckDuplicateDelegate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    chkDuplicate: function() {
        var requestedby = this.getParameter('sysparm_user');
        var delegateperson = this.getParameter('sysparm_delegate');

        var check_Delg = new GlideRecord('sys_user_delegate');
        check_Delg.addQuery('user', requestedby);
        check_Delg.addQuery('delegate', delegateperson);
        check_Delg.query();

        if (check_Delg.hasNext())
            return true;

        else
            return false;

    },

    type: 'CheckDuplicateDelegate'
});
 

 

10 REPLIES 10

Yashsvi
Kilo Sage

Hi @Sajal Chatterje,

please try below client script:

function onSubmit() {
    // Prevent form submission initially
    g_form.submitComplete = false;

    var ga = new GlideAjax('CheckDuplicateDelegate');
    ga.addParam('sysparm_name', 'chkDuplicate');
    ga.addParam('sysparm_user', g_form.getValue('requested_by'));
    ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));

    // Asynchronous call to GlideAjax
    ga.getXMLAnswer(checkDuplicateDelegate);

    // Return false to prevent form submission until GlideAjax response is received
    return false;

    function checkDuplicateDelegate(answer) {
        if (answer === "true") {
            g_form.addErrorMessage("There is already an existing delegation for this user and delegate with the same delegated items within the same time period.");
        } else {
            // Allow form submission
            g_form.submitComplete = true;
            g_form.submit();
        }
    }
}

Thank you, please make helpful if you accept the solution. 

Aditya02
Tera Guru

Hi @Sajal Chatterje,

 

The script you wrote looking good, but its not working as expected. Check once again where you are missing, write some info and alerts then you can get to know where you are lagging..

 

If my answer resolves your task, please mark this as the 'correct answer' and give hit on  like.

Thanks,
Aditya

 

 

SN_Learn
Kilo Patron
Kilo Patron

Hi @Sajal Chatterje ,

 

Here is the Client Script which is working, I have tested it in PDI:

function onSubmit() {
    if (g_scratchpad._ajaxChecked) {
        g_scratchpad._ajaxChecked = null;
        return true;
    }
    g_scratchpad._action = g_form.getActionName();
    g_scratchpad._ajaxChecked = false;
    var ga = new GlideAjax('CheckDuplicateDelegate');
    ga.addParam('sysparm_name', 'chkDuplicate');
    ga.addParam('sysparm_user', g_form.getValue('requested_by'));
    ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));
    ga.getXMLAnswer(function(answer) {
        if (answer == "true") {
            g_form.addErrorMessage("There is already an existing delegation for this user and delegate with the same delegated items within the same time period.");
            return;
        }

        g_scratchpad._ajaxChecked = true;
        if (typeof g_form.orderNow != 'undefined') {
            g_form.orderNow();
        } else {
            g_form.submit(g_scratchpad._action);
        }
    });
    return false;
}

 

Output:

 

SN_Learn_0-1720157541039.png

 

 

The detailed article for better understanding the above code is linked below:

How To: Async GlideAjax in an onSubmit script 

 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

SN_Learn
Kilo Patron
Kilo Patron

Hi @Sajal Chatterje ,

 

Thanks for marking helpful. Please also 'Accept as solution' so that it will be helpful for the future community members.

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.