on submit validation using GlideAjax in SP

muresh
Mega Contributor

Hello All,

I'm facing an issue with validation in onSubmit catalog client script. Went through different articles, but couldn't arrive at a concrete solution for this. Any help is greatly appreciated.

TIA

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@muresh 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

AnveshKumar M
Tera Sage
Tera Sage

Hi @muresh 

As Ankur stated, if you are planning to use GlideAjax in onSubmit Catalog Client Script, you need to tweak the system a bit as getXMLWait will not work in onSubmit. You can use getXML or getXMLAnswer. For example,

 

function onSubmit() {

   if (g_scratchpad.isFormValid){

    return true;

   }

   var actionName = g_form.getActionName();

   var ga = new GlideAjax("Your_Script_Include");

   ga.addParam("sysparm_name", "YourMethodName");

   ga.getXMLAnswer(function(answer) {

      if(answer == 'true'){

         g_scratchpad.isFormValid = true;

         g_form.submit(actionName);

     }else{

         g_scratchpad.isFormValid = false;

     }

   });

   return false;

}

 

By default we prevent form submit by using return false; Here we use g_scratchpad to store the validation status. And then we modify this status based on the async call return value. If the validation passes, we will submit the form using action name.

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@muresh 

remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks Ankur for your response. Now I understand these things better.

AnveshKumar M
Tera Sage
Tera Sage

Hi @muresh 

As Ankur stated, if you are planning to use GlideAjax in onSubmit Catalog Client Script, you need to tweak the system a bit as getXMLWait will not work in onSubmit. You can use getXML or getXMLAnswer. For example,

 

function onSubmit() {

   if (g_scratchpad.isFormValid){

    return true;

   }

   var actionName = g_form.getActionName();

   var ga = new GlideAjax("Your_Script_Include");

   ga.addParam("sysparm_name", "YourMethodName");

   ga.getXMLAnswer(function(answer) {

      if(answer == 'true'){

         g_scratchpad.isFormValid = true;

         g_form.submit(actionName);

     }else{

         g_scratchpad.isFormValid = false;

     }

   });

   return false;

}

 

By default we prevent form submit by using return false; Here we use g_scratchpad to store the validation status. And then we modify this status based on the async call return value. If the validation passes, we will submit the form using action name.

 

Please mark my answer helpful and accept as a solution if it helped 👍

Thanks,
Anvesh

I tried this one and with little changes I managed to make my requirement work. Thanks for the example.