How to re-Submit catalog item form in client side script with Ajax?

sthous
Giga Contributor

I have a client side script that is supposed to validate some values on the catalog item page during Submit event, and to stop submit if it fails. The problem comes in during validation because Ajax call is also involved there, and we all know that we can't stop submit from within Ajax callback. The solution for this frequent issue is described here: https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts . Basically a flag is set inside the Ajax callback and the form gets re-submitted if valid, something like this:

function onSubmit() {

   if (getValueOfTheFlag('valid')) {
      return true;
   }

  var gAjax = new GlideAjax('MyUtil');
  gAjax.getXML(MyCallBack);

  return false;

}

function MyBallBack(response) {

  if(response.success) {
      setValueOfTheFlag('valid', true);
      g_form.submit();
  } else {
      setValueOfTheFlag('valid', false);
  }

}

The problematic line here is  g_form.submit() call, which is supposed to resubmit the form after validation, however when that line is executed, I get this error message:

The g_form.submit function has no meaning on a catlog item. Perhaps you mean g_form.addToCart() or g_form.orderNow() instead
 
 
I tried also with g_form.orderNow() but nothing happens. Is there something that I'm missing here?

 

1 ACCEPTED SOLUTION

Thank you, but I just wanted to re-submit the form in order to keep the same UI experience. Turns out g_form.getControl('order_now_button').click() was all I need. It works great.

View solution in original post

7 REPLIES 7

Upender Kumar
Mega Sage

you can use getXMLWait.

 

thanks

not on the portal though

Jack Zheng3
ServiceNow Employee
ServiceNow Employee

I couldn't get g_form.OrderNow() to work on Service Portal and g_form.getControl('order_now_button').click() doesn't work either.

This article will shows how to use g_form.getActionName() and g_form.submit('name_of_action') to get it working as of the Rome release:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0779964