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

Hi @Tomas Linde,

 

Glad it worked. I simply updated the script to control the submission in order to demo the concept. Did you also update the script and remove the 'g_form.cancelSubmit()' call and replace it with 'return false'?

 

You should be able to control when the field message is displayed via the g_form.showFieldMsg(). Is it cleared when the isManager check is true?

 

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

 

I

I have made these modifications, the message is displayed correctly when it meets the criteria but the form submission is not canceled.

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');
        //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();
    }
    
}

Hi @Tomas Linde,

I'm a little confused here. Upon a closer look at the script, I can see this is an onChange script right (As in, when a value is changed on a form, we wish to look up something and prompt the user)?

 

What field is changing in order to prompt this? Additionally and importantly, what triggers the submission of the form? Are we performing the same check there (As in do you have an onSubmit script?)

 

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

I have a variable called "for_whom_are_you_requesting_this_item'" of type reference to the sys_user table in a catalog item, I want that when selecting a user with the manager keyword (which is filtered in the include script), that message is displayed and cancels the sending of the form. Maybe the function should be onSumit?

 

Hi @Tomas Linde,

 

So it seems to me we need to separate out the logic here and you will need to check onSubmit.

 

Currently, the onChange script will listen for a change to whatever field you have the onChange script listening out for. This should only show the message if the manager is set to true and that's it. Do not have a submission of any kind here - remove the g_form.submit()

 

Next step, check to see if you have any existing onSubmit scripts. You can then either update an existing onSubmit script or create a new one which is basically the same as what we have right now before the updates however of course this is of type onSubmit.

 

Make sense?

 

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