Skip to content

Instantly share code, notes, and snippets.

@D4R4
Created October 24, 2021 19:49
Show Gist options
  • Save D4R4/753cd2a4558d486f002423c9e4f2c029 to your computer and use it in GitHub Desktop.
Save D4R4/753cd2a4558d486f002423c9e4f2c029 to your computer and use it in GitHub Desktop.
OnOptionsetChange: Get OptionSet value, add N days + the days until the end of the month and set to another field
function SetDueDate(executionContext) {
var obj = Xrm.Page.getAttribute("paymenttermscode");
if (obj != null) {
var created_date = Xrm.Page.getAttribute("createdon");
if (created_date != null) {
var optionSetValue = obj.getValue();
if (optionSetValue == "100000000") {
var new_date = addDays(created_date.getValue() ,60);
new_date = addDays(new_date,getMonthDaysLeft(new_date));
Xrm.Page.getAttribute("duedate").setValue(new_date);
} else if (optionSetValue == "100000001") {
var new_date = addDays(created_date.getValue() ,45);
new_date = addDays(new_date,getMonthDaysLeft(new_date));
Xrm.Page.getAttribute("duedate").setValue(new_date);
} else if (optionSetValue == "100000002") {
Xrm.Page.getAttribute("duedate").setValue(created_date.getValue());
}
}
}
}
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + parseInt(days));
return result;
}
function getMonthDaysLeft(date){
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate() - date.getDate();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment