compare end date and start date

Community Alums
Not applicable

Hello, I have a requirement for the Record Producer. There are two variables: "Proposed Effective Date" and "Expected End Date," both of which are of the "Date" type variable.

1)Requirement is that the "Expected End Date" must be greater than the "Proposed Effective Date." If this condition is not met:

  1. Clear the value of "Expected End Date" whenever the "Proposed Effective Date" is changed.
  2. Display the error message: "Expected End Date is smaller than Proposed Effective Date."
2 REPLIES 2

Brian Lancaster
Tera Sage

You have two option for the comparison of dates.

1. No Code Data compare UI Policy

2. Script Include with client scripts

As for Clearing Expected End Date you could try doing that in a client script but know that you cannot see the previous value to verify that it was actually changed.

krishnaswamy
Tera Contributor

Hi @Community Alums 

Create a script include:

 

Script Include name: DateTimeUtils

client callable: true

Script:

var DateTimeUtils = Class.create();
DateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 compareEndDate: function() {
        var check = false;
        var startDate = this.getParameter('sysparm_start_date');
        var endDate = this.getParameter('sysparm_end_date');
        var gdt1 = new GlideDateTime(startDate);
        var gdt2 = new GlideDateTime(endDate);
        check = gdt1.before(gdt2);
        return check;
    },
    type: 'DateTimeUtils'
});

 

2. Create a client script onchange of proposed effective date

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var startdate = g_form.getValue('expected_end_date');//change the variable name
	if(startdate == '')
		{
			g_form.clearValue('proposed_effective_Date');//change the variable name
			g_form.showFieldMsg('proposed_effective_Date','Please select Expected end date first date first','error');/change the variable name			
			return;
		}
	else
		{
			var ga = new GlideAjax('global.DateTimeUtils');
			ga.addParam('sysparm_name','compareEndDate');
			ga.addParam('sysparm_start_date',startdate);
			ga.addParam('sysparm_end_date',newValue);
			ga.getXML(responseCallBack);
		}  
	function responseCallBack(response)
	{
		var ans = response.responseXML.documentElement.getAttribute('answer');
		if(ans== false || ans == 'false')
			{
				g_form.clearValue('proposed_effective_Date');//change the variable name
				g_form.showFieldMsg('proposed_effective_Date','Please select a future date','error');/change the variable name
				
			}
	}
}