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

 

Naveen Sagar S1
Mega Guru

Hi Lakshman,

Client Script : onChange (end date)

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

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

return;

}

var ga = new GlideAjax('ScriptIncludeName');

ga.addParam('sysparm_name', 'addMonths');

ga.addParam('start_date', g_form.getValue('start_date'));

ga.addParam('months_to_add', 6);

ga.getXML(getEndDate);

}

function getEndDate(response) {

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

if (answer) {
if(g_form.getValue('end_date') > answer) {
alert("End date should be less than " + answer);
}

}
}

 

Script Include :

var ScriptIncludeName = Class.create();

ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
addMonths: function() {

var start_date = this.getParameter('start_date');

var months = this.getParameter('months_to_add');
var gdt = new GlideDateTime(start_date);
gdt.addMonths(months);
return gdt.getDate();

},

}

 

This will solve your issue of 180 days.

Hi Naveen,

I am getting Null as return value from script include when I implemented yours.

Thanks

Lakshman

Hey Lakshman,

Check out this below similar thread it will help you,

https://community.servicenow.com/community?id=community_question&sys_id=409035e0dbcfb3c414d6fb243996...

 

kindly mark Correct and Helpful if Applicable.

Regards,

Indrajit.

Apologies, my bad can you replace

  • start_date with sysparm_start_date
  • months_to_add with sysparm_months_to_add

in both the client script and script include. That should work fine.