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

Mark Manders
Mega Patron

Why not just put user criteria on the item that if manager is empty, the item is not available for you?

But I think you need to move your 'cancelSubmit' out of your function and into your script itself.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

I need to access the catalog item but not select a specific user in one of the variables. I don't know if it can be done with User Criteria

Kristen Ankeny
Kilo Sage

As long as that field is required, you can clear the value in the field instead of using cancelSubmit and it will force them to choose a new person before submission.

It seems like a smart solution to me and it works, but it also removes elg_form.showFieldMsg