How can I make date field to be 3 business days from today's date,selection of any date as less than 3 days, then there should be error message?

namrata15
Kilo Contributor

Do I need to make any script include function?
please reply asap

8 REPLIES 8

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi Namrata,



You will have to write one onload script to auto populate date to be after 3 days and then one onchange script which will validate the field for less than 3 days.



1) Onload Script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  if(g_form.getValue('your date field name') == '')


  {




  var da = new GlideAjax('ClientDateTimeUtils'); // script include name


  da.addParam('sysparm_name','getDefaultEndDate');// Function name


  da.getXMLWait();


  var end_date = da.getAnswer();




  if(end_date != '')


  {


  g_form.setValue('your date field name'',end_date);


  }




  }


}





2) Script Include:



var ClientDateTimeUtils = Class.create();


ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {



getDefaultEndDate: function() {


  var startDate = gs.nowDateTime();


  var addQuantity = this.getParameter('sysparm_quant');


  var end_date = new GlideDateTime();


  end_date.setDisplayValue(startDate);


  end_date.addSeconds(259200);// Equivalent to 3 days


  end_date = end_date.getDisplayValue();


  return end_date;


  },



});





3) Onchange Script:   This should be on change of your date field



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


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


  return;


  }


  var da = new GlideAjax('ClientDateTimeUtils');


  da.addParam('sysparm_name','getDefaultEndDate');


    da.getXMLWait();


  var end_date = da.getAnswer();




  if(g_form.getValue('Your Date field name') < end_date)


  {


  alert('Date should be After 3 days from current date time');


  g_form.clearValue('Your Date field name');


  return;


  }


}





MArk Answer as Correct or helpfull


Great stuff Ashutosh, this helped me with my problem. I edited your script include to also skip over weekends and thought I would share in case anyone else wanted it. I kept your other scripts the same and I am running on Madrid:

 

 

var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDefaultEndDate: function() {
		var startDate = gs.getDate(); //get today's date
		var addQuantity = this.getParameter('sysparm_quant');
		
		var end_date = new GlideDateTime();
		end_date.setDisplayValue(startDate);
		end_date.addDaysLocalTime(3);// Equivalent to 3 days
		end_date = end_date.getDate(); //get date in MM-DD-YYYY format
	
	// get the date value from the previous variable and get its day of week integer
		var date_val = new GlideDateTime(end_date);
		date_val = date_val.getDate(); //convert to MM-DD-YYYY format (no time)
		date_val = date_val.getDayOfWeekUTC(); //get integer of date of week value
		
		// If the value equals saturday, push the desired date to monday
		if (date_val == 6) {
			end_date.addDaysLocalTime(2);
		}
		// If the value equals Sunday, push the desired date to monday
		if (date_val == 7) {
			end_date.addDaysLocalTime(1);
		}
		return end_date;
	},
	type: 'ClientDateTimeUtils'
});

dvp
Mega Sage
Mega Sage

Inorder to get the end date after 3 business days you need to use glide schedule



Here is the sample code


var startDate = new GlideDateTime('2014-01-02');
var days = 2;
var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); // Sys_id of your schedule
var end = schedule.add(startDate, dur);
gs.info(end);




Here is the more info


https://developer.servicenow.com/app.do#!/api_doc?v=geneva&id=r_ScopedGlideScheduleAdd_GlideDateTime...


namrata15
Kilo Contributor

Here I need to add the validation based on comparing with the current date


and then throw the error message on the required date field on the form