Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Date/Time Field Restriction Record Producer

Sam Ogden
Tera Guru

Hi All,

I have a date/time field on a record producer asking when the incident first occurred.

I have seen that the only way yo restrict this so people cannot add a date in the future is to have a script which validates this between the current date and the date selected when the field changes and then have a pop up message to warn the user.

I'm just not sure on how to script this?

Any help is greatly appreciated.

Thanks

37 REPLIES 37

HI Balaji,



Just tried your code above.   This however is not working as expected.   An alert shows the date selected wether this is in the past or the future and then once click ok on the alert the date remains populated with whatever was selected and you can proceed?



find_real_file.png


find_real_file.png


You have to change the field name from date to u_date(your field name) which is in bold


var cdt = g_form.getValue('u_date'); //First Date/Time field  
var dttype = 'minute'; //this can be day, hour, minute, second. By default it will return seconds.  
alert(cdt);
var ajax = new GlideAjax('ClientDateTimeUtils');  
ajax.addParam('sysparm_name','getNowDateTimeDiff');  
ajax.addParam('sysparm_fdt', cdt);  
ajax.addParam('sysparm_difftype', dttype);  
ajax.getXML(doSomething);


function doSomething(response){  
var answer = response.responseXML.documentElement.getAttribute("answer");  
if(answer >0)
{
  alert('date should not be future');
  g_form.setValue('u_date',''); /// your field name


}
}


Hi Balaji,



I thought I may have missed one, I've updated this, but I still only get the alert showing cdt variable.


I don't get any alert showing 'date should not be future' and the field does not get cleared?



I've just checked the script includes table and can't find one that is ClientDateTimeUtils


There is one that is DateTimeUtils, is this the one that should be referenced?


find_real_file.png


var DateTimeUtils = Class.create();


DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
   
    /**
      * Convert Microsoft AD integer8 DateTime format to GlideDateTime
      * Integer8 is also known as Microsoft Filetime format
      * Commonly used when importing AD user's date fields such as expiration date
      */
    int8ToGlideDateTime : function(int8Date) {
          var msDate = new Packages.org.apache.poi.hpsf.Util.filetimeToDate(int8Date);
          var gDate = new GlideDateTime();
          gDate.setValue(msDate);
          return gDate;
    },
   
    /**
      * Convert milliseconds to GlideDateTime...client usage below:
      *
      * var ga = new GlideAjax('DateTimeUtils');
      * ga.addParam('sysparm_name','msToGlideDateTime');
      * ga.addParam('sysparm_value', MILLISECONDSVALUE);
      * ga.getXMLWait();
      * var newGDT = ga.getAnswer();
      *
      * newGDT is your newly converted GlideDateTime
      */
    msToGlideDateTime : function() {
          var ms = this.getValue();
          var gDate = new GlideDateTime();
          gDate.setValue(parseInt(ms, 10));
          return gDate;
    },
   
    formatCalendarDate : function() {
          // applies a correction since the time the calendar may be sending us may be off by an hour
          // it includes the DST correction for NOW, but the date in question may not need a DST correction
          var d = GlideStringUtil.parseLong(this.getValue());
          var gDate = new GlideDateTime();
          var offsetNow = gDate.getTZOffset();
          gDate.setNumericValue(d);
          var offsetDate = gDate.getTZOffset();
          gDate.setNumericValue(d + (offsetNow - offsetDate));
          return gDate.getDisplayValue();
    },
   
    /**
      * Given a GlideDateTime get the first day of that week, used for time cards to set first day of the time card period
      * defaults to Sunday, can override by providing second parameter (1=Monday, 7=Sunday)
      *
      *@returns GlideDate
      */
    getWeekStart : function(/*GlideDateTime*/dt, /*int*/ firstDay) {
          if (!firstDay || isNaN(firstDay))
                firstDay = 7; //Sunday
         
          if (firstDay < 1 || firstDay > 7)
                firstDay = 7; //Sunday
         
          var temp = new GlideDateTime(dt);
          while (temp.getDayOfWeek() != firstDay) {
                temp.addDays(-1);
          }
         
          return temp.getLocalDate();
    }


I am sorry i thought it will be available in your instance, PFA attached XML file you can import to your instance.


Hi Balaji,



Thanks for the above. I've imported the .xml so now have the script includes of ClientDateTimeUtils.



Just tried, however still not working as expected.   Still the alert showing the cdt variable shows for both past and future dates, but when a future date is selected, no other alert shows and the variable is not cleared.



Thanks