GlideAjax and onSubmit catalog client script

Joey Wan Kenobi
Tera Guru

Hey everyone,

Im having a difficult time with getting an onSubmit catalog client script to work properly. it is meant to validate the dates entered by the user. Im ramming my head into the wall trying to find out whats going on, so i thought that maybe someone has a reason as to why what I have wont work.

 

The alert on line 23 always displays true for answer and the alert on line 32 always displays null for answer.

Is there a conflict that i dont know about when it comes to onSubmit Catalog Client Scripts and Ajax calls?

 

function onSubmit() {
   //This script validates 2 things about the times entered by the user
   var start = g_form.getValue('start_date');
   var end = g_form.getValue('end_date');

   //is start before the end?
   var ajax = new GlideAjax('AjaxUtils');
   ajax.addParam('sysparm_name','checkEndDate');
   ajax.addParam('sysparm_start_date', start);
   ajax.addParam('sysparm_end_date', end);
   ajax.getXML(checkDate);

   //is start after now?
   var ajax2 = new GlideAjax('AjaxDateDiffCalc');
   ajax2.addParam('sysparm_name','dateDiffNow');
   ajax2.addParam('sysparm_newDate', start);
   ajax2.getXML(checkStart);


   //callback functions
   function checkDate(response) {
   var answer = response.responseXML.documentElement.getAttribute("answer");
   alert ("start before end? " + answer);
   if (!answer) {
   g_form.showFieldMsg('end_date', 'The end date must occur after the start date.','error',true);
   return false;
   }
   }

   function checkStart(response) {
   var answer = response.responseXML.documentElement.getAttribute("answer");
   alert("Start is " + answer + " seconds after now");
   if (answer <= 0) {
   g_form.showFieldMsg('start_date', 'The start date must not be set to a time prior to now.','error',true);
   return false;
   }
   }
}

 

the previously existing Script includes that i was attempting to use are as follows:

AjaxDateDiffCalc:
var AjaxDateDiffCalc = Class.create();
AjaxDateDiffCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateDiffNow: function() {
var diff = gs.dateDiff(gs.nowDateTime(), this.getParameter('sysparm_newDate'), true);
return diff;
},

 

and

 

checkEndDate : function(startDate, endDate) {
         if (typeof(startDate) == 'undefined') {
               startDate = this.getParameter('sysparm_start_date');
         }
         if (typeof(endDate) == 'undefined') {
               endDate = this.getParameter('sysparm_end_date');
         }       
         return(gs.dateDiff(startDate, endDate, true) >= 0);
   },

This is from the class 'AjaxUtils'

11 REPLIES 11

Hi again,


I gave it some more thought, and you should be able to cancel the onSubmit by returning false and do the submit (or not) after the callback with something like this:


        var action = g_form.getActionName();


        gsftSubmit(action);




Take a look on the OOB client script named "Validate New Table Name" for reference.



Regards,


Dan


Using a callback in an onSubmit function does not really make much sense.   You definitely want to use a callback with onChange scripts because you do not want to lock the screen on the user while they are adding information.   When they click on the submit button, the user is done, or at least they think they are, until you evaluate the dates.   There's no functionality lost by forcing them to wait in this case.



I would stick with the getXMLwait().


dexylea
Kilo Contributor

Take a look at the below link. It's a brilliant piece of writing explaining Glide Ajax. I found it really useful, I hope it helps you too



SN Pro Tips — GlideRecord & GlideAjax: Client-Side Vs. Server-Side


jbauguess
Tera Expert

I just ran into this issue as well.   I knew the GlideAjax or even the Client Side GlideRecord were too slow on onSubmit in the catalog client script to stop the form from submitting, even if the data came back before the form moved on to the "successful order" page.   I tried circumventing this with an onChange script that did my validation.   However, my QA analyst got around that in a way I didn't think of: fill out the field that's going to be validated, and immediately press the "Order Now" button.   The check happens too late, the form submits.



I tried using getXMLWait() and GlideAjax, but I always received a null response just a second before the form submitted.   Of course, I've never used getXMLWait before, only ever getXML with a callback function as the wiki recommends.   I'll try using getXMLWait in a non-onSubmit function later to see why I'm getting a "null" response.   Using the script include function normally, I got a valid response, though.



From this there, my methodology was to find out just how the form was being submitted. I right-clicked on the Order Now button and found it calls the "orderNow" function.   So I set my catalog client script (onSubmit) to return false, and then in my call back function I called "orderNow();".   By the way, don't do that.   It causes an infinite loop.



Next, I used the "toString()" function on orderNow, and saw what it was doing.   The following lines of code are what actually submit the form:



var item_guid = gel("sysparm_item_guid");


if (item_guid)


      item_guid = item_guid.value


var catalog_guid = gel("sysparm_catalog");


if (catalog_guid)


      catalog_guid = catalog_guid.value


var catalog_view = gel("sysparm_catalog_view");


if (catalog_view)


      catalog_view = catalog_view.value;


g_cart.order(gel("sysparm_id").value, getQuantity(), item_guid, catalog_guid, catalog_view);



If you put that into the callback, after some sort of condition, it'll submit the form if your condition was met, otherwise, it stays on the catalog item until the user fixes their input.   Quick note: if you tried circumventing this as I did initially with an onChange script that validates data, make sure to disable that.   When I had both scripts active, the page submitted regardless.   Once I disabled the onChange script, it worked like a charm.



onSubmitSnippet.png



I've attached a video of this working in our dev instance.   This goes back to QA today, and to production next Tuesday.


JennyHu
Tera Guru
Tera Guru

I'm running in to the same issue.  Does anyone have a solution for this for a scoped app?

Thanks,
Jenny