End date should not be more than 6 months from start date

Lakshman12
Tera Expert

I am working on functionality as below

 I have two fields on catalog item includes – start date and end date. We do not want users to select end date not more than 6 months from start date.

For example: start date = 07/22/2020

 If End date =  1/22/2021 (technically difference is more than 180 days because of few months has 31 days ).

 Let us say if end user selects 1/23/2021 as end date, then I should alert them saying not to select as it is more than 6 months calendar days.

I would really appreciate your help. @Pradeep Sharma @Ankur Bawiskar @Chuck Tomasi 

Thanks

Lakshman

 

1 ACCEPTED SOLUTION

@Lakshman 

Below worked for me:

1) If start date is 22nd July then adding 6months to that is 22nd Jan

2) Now it depends on how 6 months you consider since some months are having 30 and 31 days.

3) I would request to check with business for exact days and that would be better.

a) So if user gives 23rd Jan then it gives alert and clears as it is not valid

b) If user gives 22nd Jan then it is ok

Client Script:

Note: Ensure you give valid variable names for start and end dates

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('DateTimeAjax');
	ga.addParam('sysparm_name', "compareDates");
	ga.addParam('sysparm_start', g_form.getValue('start_date')); // start variable
	ga.addParam('sysparm_end', g_form.getValue('end_date')); // end variable
	ga.getXMLAnswer(function(answer){
		if(answer != ''){
                        alert('End date should be within 6 months from start date');
			g_form.clearValue('end');	
		}
	});
	//Type appropriate comment here, and begin script below

}

Script Include: It should be client callable

var DateTimeAjax = Class.create();
DateTimeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	compareDates: function(){

		var start = new GlideDateTime(this.getParameter('sysparm_start'));
		var end = new GlideDateTime(this.getParameter('sysparm_end'));
		start.addMonthsUTC(6);

		if(end.getNumericValue() > start.getNumericValue()){
			return 'End Date should be withing 6 months from start';
		}
		return '';
	},

	type: 'DateTimeAjax'
});

Output:

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

31 REPLIES 31

Mark,

I have tried creating UI policy as suggested, but not working.

Note: I have written this in HR scoped application, not sure whether this matters.

Thanks

Lakshman

Can you share what you've tried exactly?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Arjun31
Giga Expert

Hello Lakshman ,

 

please paste this code in your onchange client script , no need to write server side script,

 

To check selected date is within future 6 month

 
var datesplit = (newValue+'').split('-');
var date= new Date(datesplit[2],datesplit[0]-1,datesplit[1]);

// for current date
var MaxStartDate = new Date();

// add six months in current date.
MaxStartDate.setDate(MaxStartDate.getDate() + 180);


if(date>MaxStartDate)
{

alert('selected date :'+date
+'Max date : '+sixmonthdate
+'Please select valid date');

}
else
{
alert('valid date');
}

 

 

 

 

Mark Correct and Helpful if it helps .

 

Best Regards,

Arjun Shinde

Arjun,

Thanks for sharing the script. I have already used the similar script but it is not including 31 first day of few months. for example from 7/22/2020 through 01/23/2021 -- there are 4 extra days, i.e means 184.

so, I am looking for something which can include extra days as well.

Thanks

Lakshman

 

Hi Lakshman,

Did you get chance to work on my comment?

Thanks,

Dhananjay.