Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Jon Barnes
Kilo Sage

As you probably already know, the only way to use GlideAjax in an onSubmit client script is to use getXMLWait and make a synchronous call to the server.

As you may also know, getXMLWait is flat-out not supported in Service Portal.

And so we have a problem. Many of us have use cases for checking something on the server when the user submits a form (or catalog request). But the above mutually exclusive facts have made it very difficult to create onSubmit scripts that do this and work for both native UI and Service Portal.

I am going to show you a fairly simple method for accomplishing this in a single script that works on both native and SP.

The idea is to hold a property in a persistent object on the client side that tells our submit script if the GlideAjax function has completed and is successful. So here is a simple script that you can use to accomplish this using GlideAjax and g_scratchpad:

function onSubmit() {
  if (g_scratchpad._ajaxChecked) {
    // We have run our Ajax Checks, so we can continue on
    // and let our form submission continue
    g_scratchpad._ajaxChecked = null;
    return true;
  }
  // use this line below if you want to store the specific action name
  g_scratchpad._action = g_form.getActionName();
  g_scratchpad._ajaxChecked = false;
  var ga = new GlideAjax('MyAjaxScriptInclude');
  ga.addParam('sysparm_name', 'myAjaxFunction');
  ga.getXMLAnswer(function(answer) {
    // I made this a simple check of a true/false result
    // but you can change this to check whatever suits your business case
    // and base it on what gets returned from your script include
    if (answer == "false") {
      // we didn't pass our checks, so alert the user and quit
      alert("didn't work");
      return;
    }
    // it worked! now we can resubmit the form with the 
    // property in place to allow us to continue
    // so once we resubmit, it will re-run this function but will return true
    // at line 5 of this script
    g_scratchpad._ajaxChecked = true;
    if (typeof g_form.orderNow != 'undefined') {
      // this is a catalog item
      g_form.orderNow();
    } else {
      // this will resubmit the form using the saved 
      // ui action that was originally clicked
      g_form.submit(g_scratchpad._action);
    }
  });
  // always return false if we get to this point
  return false;
}

Let me know if any questions!

12 Comments