7 Replies · Latest reply on Nov 14, 2017 8:38 AM by David Wilhelm

    Client script with datetimeutil script include returns null value

      Hi everyone,

       

      I have the following code that works fine when I press Shift, CTRL, ALT, J on my custom table form.

      Capture.PNG

      However, when I have this same code in my client script for the form, the value returned is NaN.

       

      Client script:

      function onLoad() {
      var cdt = g_form.getValue('u_start_date'); //First Date/Time field
      var sdt = g_form.getValue('u_end_date'); //Second Date/Time field
      var dttype = 'day';
      
      var ga = new GlideAjax('MRAClientDateTimeUtils');
      ga.addParam('sysparm_name','getDateTimeDiff');
      ga.addParam('sysparm_fdt', cdt);
      ga.addParam('sysparm_sdt', sdt);
      ga.addParam('sysparm_difftype', dttype);
      ga.getXMLWait();
      var newGDT = ga.getAnswer();
      alert(parseInt(parseInt(newGDT) / 30));
      }
      

       

      script include:

      var ClientDateTimeUtils = Class.create();
      ClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
      
      //Diff the amount of time between two different Date/Time fields
      //params = sysparm_fdt (the first date/time field), sysparm_sdt (second date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)
      getDateTimeDiff: function(){
      var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
      var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field
      var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day
      var diff = gs.dateDiff(firstDT, secondDT, true);
      var timediff = this._calcDateDiff(diffTYPE, diff);
      //return "getDateTimeDiff: FIRST DT: " + firstDT + " -SECOND DT: " + secondDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;
      return timediff;
      },
      
      //Private function to calculate the date difference return result in second, minute, hour, day.
      _calcDateDiff: function(diffTYPE, seconds){
      var thisdiff;
      if (diffTYPE == "day"){thisdiff = seconds/86400;}
      else if (diffTYPE == "hour"){thisdiff = seconds/3600;}
      else if (diffTYPE == "minute"){thisdiff = seconds/60;}
      else if (diffTYPE == "second"){thisdiff = seconds;}
      else {thisdiff = seconds;}
      return thisdiff;
      }
      
      });
      

       

      The script include is Client callable. The script include, form, and client script are all in the same application scope.

       

      Anyone know why the value returned is NaN from the client script but brings back the value of 73 when ran manually on the form in the javascript editor?

        • Re: Client script with datetimeutil script include returns null value
          Christopher Johnson

          From what I know Ajax returns an xml string. I have encountered issues when I am in a different application scope. If your form is part of a custom application and your Script Include is in the Global scope, you should be able to add "global." in front of your script include call

           

          var ga = new GlideAjax('global.MRAClientDateTimeUtils');

            • Re: Client script with datetimeutil script include returns null value
              David Wilhelm

              Thanks for the reply!

               

              The script include MRAClientDateTimeUtils is actually in the same application scope as the client script and form.

              I did try to change the client script to your suggestion and I am still getting back a NaN value.

               

              Additionally, it dawned on me it may be a domain issue. The script include is probably in the global domain and the script include is in our TOP domain, which is one under global. I recreated the client script in the global domain and tried your suggestion and the call to the same application scope script include and all still bring back the value of NaN.

                • Re: Client script with datetimeutil script include returns null value
                  Christopher Johnson

                  The red flag that I see is the glide system call in your script include:

                  var diff = gs.dateDiff(firstDT, secondDT, true); 

                   

                  I have found that scoped script includes do not play nice with glide system (gs) calls. You may want to change the script include to run from the global application scope, or use a different method instead of dateDiff. I think that GlideDuration has methods that run within other scopes. I think an equivalent function would be  subtract. Here is a function that I wrote in a scoped app that uses it:

                   

                      compareDates: function() {
                          var firstDT = this.getParameter('sysparm_fdt');
                          var secondDT = this.getParameter('sysparm_sdt');
                          var fstDT = new GlideDateTime(firstDT);
                          var secDT = new GlideDateTime(secondDT);
                          var dur = new GlideDuration();
                          dur = GlideDateTime.subtract(fstDT, secDT);
                          var chkVal = parseInt(dur.getNumericValue());
                          if (chkVal < 0) {
                              return false;
                          } else {
                              return true;
                          }
                      },
                  
                  1 of 1 people found this helpful
                    • Re: Client script with datetimeutil script include returns null value
                      David Wilhelm

                      It seems I am getting a null value no matter what I have in the script include.

                       

                      Here is my current script calling the script include:

                       

                      var ga = new GlideAjax('MRAClientDateTimeUtils');

                      ga.addParam('sysparm_name','getDateTimeDiff');

                      ga.getXMLWait();

                      var newGDT = ga.getAnswer();

                      alert(newGDT);

                       

                      Here is the script include. I'm simply trying to return 'Hello World'. I still get a null value.

                       

                      var MRAClientDateTimeUtils = Class.create(); 

                      MRAClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, { 

                       

                      getDateTimeDiff: function(){ 

                      return 'Hello World';

                      }

                       

                      });

                        • Re: Client script with datetimeutil script include returns null value
                          dvp

                          getXMLWait is not supported in scoped apps

                          Here is an article that talks more about unsupported api's in scoped

                          Scoped Applications and Client Scripts: A Primer

                          1 of 1 people found this helpful
                            • Re: Client script with datetimeutil script include returns null value
                              David Wilhelm

                              I viewed that document and I still can't get it to work. I am using GlideAjax and I use getXML which is asynchronous. My form, client script, and script include are all in the same application scope. I still always get back a null value. It should return with "Hello World". Here are my updated client script and script include code.

                               

                              Client Script:

                              function onLoad() {

                              var cdt = g_form.getValue('u_start_date'); //First Date/Time field

                              var sdt = g_form.getValue('u_end_date'); //Second Date/Time field

                              var dttype = 'day';

                               

                              var ga = new GlideAjax('MRAClientDateTimeUtils');

                              ga.addParam('sysparm_name','getDateTimeDiff');

                              ga.addParam('sysparm_fdt', cdt);

                              ga.addParam('sysparm_sdt', sdt);

                              ga.addParam('sysparm_difftype', dttype);

                              ga.getXML(getAnswer);

                              }

                               

                              function getAnswer (response){

                              var answer = response.responseXML.documentElement.getAttribute("answer");

                              alert(answer);

                              //alert(parseInt(parseInt(newGDT) / 30));

                              }

                               

                              Script include:

                              var MRAClientDateTimeUtils = Class.create();

                              MRAClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

                               

                              getDateTimeDiff: function(){

                              var timediff = 'Hello World';

                              return timediff;

                              }

                              });

                                • Re: Client script with datetimeutil script include returns null value
                                  David Wilhelm

                                  OK, I finally got it to work. The above did not work because I had two client scripts named the same thing. I deleted the one.

                                   

                                  As Christoper stated above, gs.dateDiff does not work with scoped applications. I had to use GlideDateTime.subtract, duration, and .getDayPart to get the number of months. I also had to add global. in front of AbstarctAjaxProcessor for it to work.

                                   

                                  The below code works in my script include.

                                   

                                  var MRAClientDateTimeUtils = Class.create();

                                  MRAClientDateTimeUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

                                   

                                  getDateTimeDiff: function(){

                                  var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field

                                  var secondDT = this.getParameter('sysparm_sdt'); // Second Date-Time Field

                                  var diffTYPE = this.getParameter('sysparm_difftype'); // Date-Time Type to return the answer as. Can be second, minute, hour, day

                                   

                                  var gdt1 = new GlideDateTime(firstDT);

                                  var gdt2 = new GlideDateTime(secondDT);

                                  var dur = new GlideDuration();

                                   

                                  dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

                                  return parseInt(dur.getDayPart()/30);

                                  }

                                  });

                                   

                                  Thank you very much for the help!

                                  1 of 1 people found this helpful