Alternative for gs.nowDateTime() in Catalog Client Script?

LG2
Mega Expert

Hi,

 

We have a variable in a catalog item called oos_end which is a date/time field.

 

We need to stop people setting this field to a date in the past - apparently common sense isn't the answer. nor is sacking the culprits - so I'm looking at a Catalog Client Script - type "on Change" - running on that catalog item and variable

 

I can't use gs.nowDateTime() so can anyone suggest an alternative please?   I've had a trawl through other client scripts we have but they seem to compare the values in two existing fields/variables rather than "now".

 

thanks in advance for any pointers

 

(Current release: Berlin)

 

---------------------

 

function onChange(control, oldValue, newValue, isLoading) {

    if(isLoading){

    return;

    }

    var now = ?????? ;

 

    if(newValue < now){

    alert("Polite message about setting dates in the past etc");

        g_form.setValue("variables.oos_end",'');

    }

 

}

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

It sounds as if you want to check the date the person selected in the variable and warn the user if they selected a date in the past. Using the suggestion by Lam_Hoang, we can perform a date/time check on the selected value and return a true/false value that we can throw an alert with.



Step 01. Create a script include that you can call from your Catalog Client Script:


Name: dateTimeUtilAjax


Client callable: true


Script:


var dateTimeUtilAjax = Class.create();


dateTimeUtilAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      checkNowDateTime: function() {


              var firstDT = this.getParameter('sysparm_fdt');


              var nowDT = gs.nowDateTime();


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


              if (diff > 0) {


                      return false;


              } else {


                      return true;


              }


      },



      getNowDateTime: function() {


              return gs.nowDateTime();


      }



});



Step 02. Create your Catalog Client Script:


Name: Warn if oos_end is before now


Applies to: A Catalog Item


Type: onChange


Catalog item: ***WHATEVER YOUR ITEM IS***


Variable name: oos_end


Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading || newValue == '') {


              return;


      }


      if (newValue != oldValue) {


              var startDate = newValue;


              var ga = new GlideAjax('dateTimeUtilAjax');


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


              ga.addParam('sysparm_fdt', startDate);


              ga.getXML(checkDate);


      }


}



function checkDate(response) {


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


      if (answer == 'false') {


              alert("You must select a date in the future");


              g_form.setValue('oos_end', '');


      }


}



Let me know if you are still having difficulty.


View solution in original post

7 REPLIES 7

prdelong
Kilo Guru

If you want date/time values for you client script, I would create an Display BR to store those values in g_scratchpad. That will let you reference whatever server side code you need. Here are some examples:



Script Lab - ServiceNow Wiki


Tip: Server side functions and client scripts


https://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Example:_g_scratchpad


Lam_Hoang
Kilo Expert

easiest way is to create an GlideAjax.



an client script to call the scipt include, like



var ga = new GlideAjax('LSMC_utils');


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


ga.getXML(parseXML);



function parseXML(response) {


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


    alert(answer);


}



and a script include like:



var LSMC_utils = Class.create();


LSMC_utils.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getnowDateTime: function() {


          return gs.nowDateTime();


    }



       


});bla bla glideajax.PNG


ccajohnson
Kilo Sage

It sounds as if you want to check the date the person selected in the variable and warn the user if they selected a date in the past. Using the suggestion by Lam_Hoang, we can perform a date/time check on the selected value and return a true/false value that we can throw an alert with.



Step 01. Create a script include that you can call from your Catalog Client Script:


Name: dateTimeUtilAjax


Client callable: true


Script:


var dateTimeUtilAjax = Class.create();


dateTimeUtilAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      checkNowDateTime: function() {


              var firstDT = this.getParameter('sysparm_fdt');


              var nowDT = gs.nowDateTime();


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


              if (diff > 0) {


                      return false;


              } else {


                      return true;


              }


      },



      getNowDateTime: function() {


              return gs.nowDateTime();


      }



});



Step 02. Create your Catalog Client Script:


Name: Warn if oos_end is before now


Applies to: A Catalog Item


Type: onChange


Catalog item: ***WHATEVER YOUR ITEM IS***


Variable name: oos_end


Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


      if (isLoading || newValue == '') {


              return;


      }


      if (newValue != oldValue) {


              var startDate = newValue;


              var ga = new GlideAjax('dateTimeUtilAjax');


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


              ga.addParam('sysparm_fdt', startDate);


              ga.getXML(checkDate);


      }


}



function checkDate(response) {


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


      if (answer == 'false') {


              alert("You must select a date in the future");


              g_form.setValue('oos_end', '');


      }


}



Let me know if you are still having difficulty.


Thank you so much - (and everyone else for their replies too) - this has worked an absolute treat!   No more relying on common sense...   many thanks!