I am trying to achieve the following in JavaScript for CRM 2013
If a date field
gteq_expirydate
is within the next 45 days set an alert onchange.
How do I do it?
I am trying to achieve the following in JavaScript for CRM 2013
If a date field
gteq_expirydate
is within the next 45 days set an alert onchange.
How do I do it?
Add days to JavaScript Date use this example to do operation on javascript dates, then run a simple comparison.
var crmDate = Xrm.Page.getAttribute("gteq_passportexpirydate").getValue();
var today = new Date();
var tomorrow = new Date(today);
tomorrow.setDate(today.getDate()+45);
if(crmDate < tomorrow && crmDate >= today){
alert("care");
}
If you need to compare the old value of the date field with the new value that you write in the date field, you will need another field called "old date field" to run the comparison, otherwise the value you will retrieve will be the new one.
Take a look at Moment JS to assist with this requirement (no connection to the project, just have found it very helpful in CRM for these types of requirements.)
You need to to add the moment.js library and an OnChange event for the field similar to this:
function checkDateOnChange (e) {
var dateField = e.getEventSource();
var newDate = e.getValue();
if (moment.isDate(newDate) && moment(newDate).isBefore(moment().Add(45, 'days')))
alert("Date is 45 or less days in the future.");
}
Besides simplicity of implementation it makes the code very readable when you look at it in the future because of the fluent style.