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

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why not have single GlideAjax and perform both the checks within 1 function only

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Community Alums 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Runjay Patel
Giga Sage

Hi @Community Alums ,

 

You need to 2 correction in your code.

  1. instead of getXMLAnswer you should use getXMLWait to make sure your both ajax call execute one after another and not in parallel.
  2. instead of useing return in every block use should use one flag to store the true/false/empty value and at the end use return with flag like below

 

var flag =true;
if (answer2 === 'false')
flag ='';

return flag;

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

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).