prevent a date field being a date prior to today

patricklatella
Mega Sage

Hi everyone!

I have a date field Due date (due_date) on a custom table.   I need to prevent entering a date into this field that is prior to today.   To achieve this I've created a client script and an accompanying script include.   I believe I am close, anyone see why it's not working?   thanks!

my onChange client script is:

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

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

          return;  

    }  

 

  //make sure they are not asking for a due date in the past (before now)  

  var ga = new GlideAjax("NowDateCalc"); //name of the called script include  

  ga.addParam("sysparm_name", "getNow"); //name of the function in the script include  

  ga.getXML(getDate); //callback function  

}  

 

function getDate(response) {  

  var due_date = g_form.getValue('due_date');   //due_date is the name of the field on the table

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

 

  if (due_date < rightNow) {  

  alert("Due date cannot be earlier than today.");  

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

  }  

}

and my script include, "NowDateCalc" is:

var NowDateCalc = Class.create();

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

getNow : function() {

 

  return gs.now();

},

      type: 'NowDateCalc'

});

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi,



You can simply do this with the help of UI Policy itself without much need of any script as shown below:



For example I have done this for Planned End Date on Change Request Table as shown below:



find_real_file.png



find_real_file.png



Result:



find_real_file.png



Post this Alert Message it will clear out the Field Value selected by the User also. Replace your Table Name and Field Name in the UI Policy shown above accordingly. This would work on click itself when the User selects the Date i.e. on Change of the field Value.



Hope this helps.Mark the answer as correct/helpful based on impact.




Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

18 REPLIES 18

Chuck Tomasi
Tera Patron

Hi Patrick,



If you want a script-less way of doing this, consider a BEFORE business rule. You can do this in the condition builder



Start Date | at or before | Current minute



Then in the Actions section tell it to abort with a message.


balaji_charapal
Kilo Guru

You can try with below code..


if(g_form.getValue('end_date') !='' )


  {


  var dFormat = g_user_date_format;


  var tFormat = g_user_date_time_format;


  var end = getDateFromFormat(g_form.getValue('end_date'),dFormat);


    var todayDate = formatDate(new Date(),dFormat);


  var today = getDateFromFormat(formatDate(new Date(),dFormat),dFormat);


  var sDate=getDateFromFormat(g_form.getValue('start_date'),dFormat);


  if (today < end) {


  alert(getMessage("Desired End Date has to be a date in the future"));


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


  }


}


small correction but its import.




if(g_form.getValue('end_date') !='' )


  {


  var dFormat = g_user_date_format;


  var tFormat = g_user_date_time_format;


  var end = getDateFromFormat(g_form.getValue('end_date'),dFormat);


    var todayDate = formatDate(new Date(),dFormat);


  var today = getDateFromFormat(formatDate(new Date(),dFormat),dFormat);


  if (today > end) {


  alert(getMessage("Desired End Date has to be a date in the future"));


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


  }


}



find_real_file.png


patricklatella
Mega Sage

Hello Chuck, thanks and I tried a business rule, but ideally I want the user to be alerted before they save/update...business rule can't do that, correct?