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

Okay, so on the one hand I show the message and on the other I manage the submission of the form.
The fact is that I have not created any more catalog client scripts for this catalog item, so I could create another one from scratch to manage the shipment. Would it go against one out of the box that is being used in the portal?

I have created a new client script but the portal returns "ErrorThere is a JavaScript error in your browser console" and allows me to request the catalog item

function onSubmit() {
  
  var ajaxProcessor = new ALOCatalogItemOtherSoftwareAJAX();
  var isManager = ajaxProcessor.isManager();

  // If isManager returns true, cancel the catalog item submission
  if (isManager) {
    return false; // Prevent form submission
  }

  return true; // Allow form submission to proceed
}

It is possible to show the field message. You should clear the value then show field message, it should keep your field message.

I have tried it and it works until I enter the portal, which shows me this message

"ErrorThere is a JavaScript error in your browser console"

Can you show the set up of the script at this point?