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

Hello @SN_Learn ,

 

Everything working fine and it also showing the message. But the issue is although its showing error message , it is redirecting to different page. We don't want to redirect to different page if it is not passing the validations.How can I handle this???

 

Thanks!

Hi @Sajal Chatterje , 

 

I tried in PDI just now with the below code:

 

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'
});

 

onSubmit Client Script:

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-1721835450282.png

 

 

Let me know if the issue still persist, also please share the screenshot where it is redirecting to?

If it is a record producer check the script part if there is anything defined over there such as portal.redirect

 

Mark this as Helpful / Accept the Solution if this helps

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

Hello @SN_Learn ,

Yes, it is working with two parameters but when I increased the parameters to 4.Its not working Like the below I added 4 parameters.Could you please check with 4 parameters??

 

    ga.addParam('sysparm_user', g_form.getValue('requested_by'));
    ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));
    ga.addParam('sysparm_startdate', g_form.getValue('start_date'));
    ga.addParam('sysparm_enddate', g_form.getValue('end_date'));
 
 
Thanks,
Sajal
 

Thanks for providing the details.

 

Could you please share the Client script and Script Include to debug it? Also start date and end date is date type field or date/time field and what is the requirement?

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

Hello @SN_Learn ,

Client script

function onSubmit() {

    //Type appropriate comment here, and begin script below
    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.addParam('sysparm_startdate', g_form.getValue('start_date'));
    ga.addParam('sysparm_enddate', g_form.getValue('end_date'));

    //return false;
    ga.getXML(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;
}
 
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 startdate = this.getParameter('sysparm_startdate');
        var enddate = this.getParameter('sysparm_enddate');

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

        if (check_Delg.hasNext())
            return true;

        else
            return false;

    },

    type: 'CheckDuplicateDelegate'
});