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

Try changing below 2 lines of code(bold) in script include.



getNowDateTimeDiff: function(){


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


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


  var fstDT = new GlideDateTime(firstDT);


    var diff = gs.dateDiff(gs.nowDateTime(), fstDT.getDisplayValue(), true);


  var timediff = this._calcDateDiff(diffTYPE, diff);


  //return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;


  return timediff;


  },


Changed the above but still getting the same outcome?



var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



//Takes a Single Date/Time Field and returns its time difference from nowDateTime().


//params = sysparm_fdt (the first date/time field), sysparm_difftype (time based format to return result. See "_calcDateDiff" function comments)


getNowDateTimeDiff: function(){


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


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


  var fstDT = new GlideDateTime(firstDT);


  var diff = gs.dateDiff(gs.nowDateTime(), fstDT.getDisplayValue(), true);


  var timediff = this._calcDateDiff(diffTYPE, diff);


  //return "getNowDateTimeDiff: FIRST DT: " + firstDT + " -DIFFTYPE: " + diffTYPE + " -TIME DIFF: " + timediff;


  return timediff;


},


Hi Balaji,



Just received some feedback internally and currently u_date variable is set to a date/time field - we need to change this to just a date field, will this need changing in the scripts?



Thanks


SanjivMeher
Mega Patron
Mega Patron

Write a script inclde


ValidateStartDate: function() {
var se_start_date = this.getParameter('startdate');
var opened_date = gs.now();
var currentDateTime = new GlideDateTime();
currentDateTime.setDisplayValue(opened_date,"MMM d, yyyy");
var start_date = new GlideDateTime();
start_date.setDisplayValue(se_start_date,"MMM d, yyyy");
if (se_start_date!='' && start_date<currentDateTime)
{
return 1;
}

}




And then write a on change client script



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


  g_form.hideFieldMsg('requested_by_date', true);




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


  return;


  }



  var startdate = g_form.getValue('requested_by_date'); //Choose the field to add time from




  var ajax = new GlideAjax('ServerBuild');


  ajax.addParam('sysparm_name', 'ValidateStartDate');


ajax.addParam('startdate ', startdate );


  ajax.getXML(validateDate);


  //Type appropriate comment here, and begin script below


}




function validateDate(response){


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



  else if (answer==1)


  g_form.showFieldMsg('requested_by_date',"The start date can't be in past",'error');


}




Same client script should be in onSubmit script



Please mark this response as correct or helpful if it assisted you with your question.

Alikutty A
Tera Sage

Hi Sam,



You can add this simple script on the record producer script to satisfy your requirement



var initialDate = new GlideDateTime(producer.initial_date).getDate();   //Replace initial_date with the variable name in your record producer


var today = new GlideDateTime().getDate();


var diff = gs.dateDiff(today,initialDate, true);



if(diff > 0){


  gs.addInfoMessage("You cannot enter a date greater than today");


  gs.setRedirect("/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=sys_id_of_record_producer"); //Replace with sys_id of RP


  current.setAbortAction(true);


}



Thank You


Please Hit Like, Helpful or Correct depending on the impact of response