Cancel form submission

Tomas Linde
Tera Expert

Hello everyone,

I have this catalog client script that works correctly except that it does not cancel the submission of the form if the condition is met, any ideas.
Greetings

function onChange(control, oldValue, newValue, isLoading) {

    // If the field is loading, do nothing.
    if (isLoading) {
        return;
    }

    var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');

    var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
    ga.addParam('sysparm_name', 'isManager');
    ga.addParam('sysparm_user', requestedFor);
    ga.getXML(checkManager);
}

function checkManager(response) {

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

    // If the user is a manager, display an error message and cancel the form submission.
    if (isManager == 'true') {
        g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
        // Cancel form submission
        g_form.cancelSubmit();
    }

    // If the user is not a manager, clear the error message.
    else {
        g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
    }
}
2 ACCEPTED SOLUTIONS

Robbie
Kilo Patron
Kilo Patron

Hi @Tomas Linde,

 

The getXML function will not wait for the AJAX to run. You therefore need to stop submitting the form after the getXML command and handle the submission inside the checkManager function (AJAX code). Please try the below code with a few tweaks:

 

 

 

function onChange(control, oldValue, newValue, isLoading) {

    // If the field is loading, do nothing.
    if (isLoading) {
        return;
    }

    var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');

    var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
    ga.addParam('sysparm_name', 'isManager');
    ga.addParam('sysparm_user', requestedFor);
    ga.getXML(checkManager);
    return false;
}

function checkManager(response) {

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

    // If the user is a manager, display an error message and cancel the form submission.
    if (isManager == 'true') {
        g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
        // Cancel form submission
        return false;
    }

    // If the user is not a manager, clear the error message.
    else {
        g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
        //if there is no other condition to check other than the manager field - control submission here. You can also control this with an if statement if required 
        g_form.submit();
    }
    
}

 

 

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

 

View solution in original post

Try taking the "return false" right after your getXML out. If that doesn't work - open the console log - many times that will show you exactly where the error lies (for example, I've see that a variable being used is coming back undefined and it's causing the error). 

View solution in original post

23 REPLIES 23

Of course

Captura de pantalla 2024-02-22 a las 17.38.35.png

And what does the script step look like right now?

 

The false return is common and is commented because it does not work

function onChange(control, oldValue, newValue, isLoading) {

    // If the field is loading, do nothing.
    if (isLoading) {
        return;
    }

    var requestedFor = g_form.getValue('for_whom_are_you_requesting_this_item');

    var ga = new GlideAjax('ALOCatalogItemOtherSoftwareAJAX');
    ga.addParam('sysparm_name', 'isManager');
    ga.addParam('sysparm_user', requestedFor);
    ga.getXML(checkManager);
    return false;
}

function checkManager(response) {

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

    // If the user is a manager, display an error message and cancel the form submission.
    if (isManager == 'true') {
        g_form.showFieldMsg('for_whom_are_you_requesting_this_item', 'The user is not allowed.', 'error');
		//return false
    }

    // If the user is not a manager, clear the error message.
    else {
        g_form.clearFieldMsg('for_whom_are_you_requesting_this_item');
    }
    
}

Try taking the "return false" right after your getXML out. If that doesn't work - open the console log - many times that will show you exactly where the error lies (for example, I've see that a variable being used is coming back undefined and it's causing the error). 

Community Alums
Not applicable

Hi @Tomas Linde ,

To abort in client script just use the line below

return false;