return false not working in glideajax in onsubmit

sushmachava
Tera Contributor

could pls let me know what is wrong in below client script, how this can be updated further form is not submitting after validation is set to true ... in the below script i have also used a hidden checkbox variable if condition  satisfied i am setting validation as false in the else case it is setting ture based on this check i am trying to submit the form but still that is not submitting

    // Perform GlideAjax call to check user status
        var ga = new GlideAjax('CheckUserStatus');
        ga.addParam('sysparm_name', 'checkStatus');
        ga.addParam('sys_id', userIDsToCheck.join(','));
        ga.getXML(CheckValidation);
        return false;        
        function CheckValidation(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            answer = answer.toString().split(';');
           
            if (answer[1] > 0) {
                alert('inactive user count: ' + answer[1]);
                alert('Inactive User IDs: ' + answer[0]);
                g_form.clearValue('dl_creation_group_members');
                g_form.addErrorMessage('Some user IDs are inactive. Please correct the input.');
                // Set checkbox to false
                g_form.setValue('validation', 'false');
            } else {
                // Set checkbox to true
                g_form.setValue('validation', 'true');
            }
            if (g_form.getValue('validation') === 'true') {
                g_form.submit();
            }
        }
    }
2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I haven't run the code, but the return false on line 5 is preventing the form submission.  Try this approach instead:

 

var ga = new GlideAjax('CheckUserStatus');
ga.addParam('sysparm_name', 'checkStatus');
ga.addParam('sys_id', userIDsToCheck.join(','));
ga.getXML(CheckValidation);
                
    function CheckValidation(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        answer = answer.toString().split(';');
           
        if (answer[1] > 0) {
                alert('inactive user count: ' + answer[1]);
                alert('Inactive User IDs: ' + answer[0]);
                g_form.clearValue('dl_creation_group_members');
                g_form.addErrorMessage('Some user IDs are inactive. Please correct the input.');
                return false;
        } 
    }

 

Assuming you are running this in an onSubmit client script, you don't need to do anything to submit the form if the validation check passes, as this is already built into the function of this client script.

_Tushar Soni
Tera Guru
function onSubmit() {
    var userIDsToCheck = []; // Populate this array with actual user IDs

    var ga = new GlideAjax('CheckUserStatus');
    ga.addParam('sysparm_name', 'checkStatus');
    ga.addParam('sys_id', userIDsToCheck.join(','));

    ga.getXMLAnswer(function(response) {
        var answer = response.split(';');
        
        if (answer[1] > 0) {
            alert('Inactive user count: ' + answer[1]);
            alert('Inactive User IDs: ' + answer[0]);
            g_form.clearValue('dl_creation_group_members');
            g_form.addErrorMessage('Some user IDs are inactive. Please correct the input.');
            // Set checkbox to false
            g_form.setValue('validation', 'false');
            // Prevent form submission
            g_form.setAbortAction(true);
        } else {
            // Set checkbox to true
            g_form.setValue('validation', 'true');
            // Allow form submission
            g_form.setAbortAction(false);
        }
    });
    // Return false to prevent the form from submitting immediately
    return false;
}

 

If you found my response helpful, please consider marking it as "Helpful" or "Accept Solution." Thank you!