Double-click Required for catalog Submission After Multiple GlideAjax Checks - How to Resolve?

Community Alums
Not applicable

I’m working on a submission of catalog form where I have two GlideAjax script includes that need to run before submitting the form. The logic works well for individual checks, but there’s an issue when both checks return true.

 

When both conditions are met (both checks are successful), the form requires two clicks on the submit button for the form to submit. On the first click, it fails silently and requires a second click to submit. I’m unsure why this is happening, as the first check is followed by the second check, and both are only completed successfully if conditions match.

FYR: Both script includes are working fine.

Here’s the code I’m currently using to validate both checks:

 

function onSubmit() {
    var up = g_form.getValue('test_cmdb_updated_status');
    if (up == 'Decommissioned') {
        if (g_scratchpad.isFormValid)
            return true;
        // Check 1: First Script
        var sd1 = g_form.getValue('test_cmdb_app_decom');
        var ga1 = new GlideAjax('decom_app_access');
        ga1.addParam('sysparm_name', 'returnloggedUder');
        ga1.addParam('sysparm_catitem', sd1);
        ga1.getXMLAnswer(function(answer1) {
            if (answer1 === 'false') {
                alert("1st checks");
                g_form.addErrorMessage('You are not authorized to submit this eForm.');
                return; // Block submission 
            }
            // Proceed to Check 2: Second Script
            var sd2 = g_form.getValue('test_cmdb_app_decom');
            var ga2 = new GlideAjax('disable_decomission_for_active_server_app');
            ga2.addParam('sysparm_name', 'get_details');
            ga2.addParam('sysparm_user_name', sd2);
            ga2.getXMLAnswer(function(answer2) {
                if (answer2 === 'false') {
                    alert("2nd checks");
                    g_form.addErrorMessage('Please ensure that the application has been removed from the active server.');
                    return; // Block submission if the second check fails
                }
                var actionName = g_form.getActionName();
                g_scratchpad.isFormValid = true;
                g_form.submit(actionName); // Submit the form
                return true;
            });

        });

        return false; // Prevent default form submission
    }
    return true;
}

 

 

The problem:

 

1. When both conditions (answer1 && answer2) return true from their respective script includes, the form submission doesn't happen on the first click, but works on the second click.

 

How can I ensure that the form is successfully submitted after the first click, once both checks are successfully completed? I would appreciate your assistance in solving the issue, thank you!

1 ACCEPTED SOLUTION

Nick Parsons
Mega Sage

The one issue I can spot that may be causing your code to behave strangely is that you're calling getActionName() asynchronously. ServiceNow expects this to be called synchronously, so rather than calling this method inside of your GlideAjax callback, call it at the start of your script before you initiate any GlideAjax calls and store the result in a variable, ie:

var actionName = g_form.getActionName();
var sd1 = ...

and then use this variable from the closure in your callback (see code comments):

// remove the getActionName() call from here
g_scratchpad.isFormValid = true;
g_form.submit(actionName); // Submit the form (uses variable from outer-scope)
return; // no need to return true here, this doesn't return to your onSubmit function, it returns the the callback, so returning true/false here doesn't impact the submission. 

 

PS: I wouldn't suggest using getXMLWait if you don't have to, since it's a synchronous method and it's going to block your UI thread, meaning the browser won't respond to any user events such as hovers etc. and will make it feel like your user's browser is momentarily frozen (ServiceNow also do not recommend using this in their docs) For something like this, you should be able to use asynchronous methods like you are currently, and ServiceNow does say/show how it's possible in this article KB0779964 (which is what you're basically doing now).

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

thanks @Nick Parsons, Its working as expected now!