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

sachin_namjoshi
Kilo Patron
Kilo Patron

You need to use cart API to submit catalog item and call this cart API from your script include.

This will submit catalog item.

 

Also, you can use OOB service catalog API to submit order using catalog item.

You can call this service catalog API from your script include.

 

find_real_file.png

 

Regards,

Sachin

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.

g_form.getControl('order_now_button').click(); is not working for me. Can you give me an alternative. g_form.submit is working for portal but not for catalog item CMS. I want some alternative of g_form.submit which works in both.

 

Here is the callback function. Please help

 

function HelloWorldParse(response) {
if(ans1 =="success")
{
g_form.setValue('check_flag', 'true');
g_form.getControl('order_now_button').click();
}
else{

alert("No field values have changed");
g_form.clearValue('application_name');
g_form.clearValue('type_of_operation');
return false;
}
}

ssss22
Tera Contributor

Thanks thats all what I need too 🙂