New date does not overwrite executionContext date retrived.
Hi!
I have a javascript that’s calculating the end date based off the start date. The function triggers onChange for the start date field. But also before that, sets a default time for both start- and end date. This only happens when it is a create form.
The issue here is that when opening a create form, I retrieve the start and end date from the executionContext and sets the default time for both fields. Afterwards the onChange function runs, where we once again retrieves the start and end date fields from executionContext, calculates the new end date based off the retrieved start date, and makes sure that the previous end date time is set on the new end date. The time that I get is the first one caught by the executionContext and not the default time that I updated it to.
Is there some way for me to get the new value (the default time) and fetch that in the onChange function via the executionContext, without having to create a new separate js? When is the executionContext updated, because the form does get the default end time for a millisecond before getting the onChange value.
if (typeof (FA) == "undefined") { FA = {}; }
FA.Event = {
formContext: null,
OnLoad: function (executionContext) {
this.formContext = executionContext.getFormContext();
// Run on Create form
if (this.formContext.ui.getFormType() === 1) {
if (this.formContext.getAttribute("course_id").getValue() != null) {
FA.Event.SetEndDate(executionContext);
}
if (this.formContext.getAttribute("startdate").getValue() != null) {
FA.Event.SetDefaultStartTime(executionContext);
} else {
alert("startdate was null");
}
if (this.formContext.getAttribute("enddate").getValue() != null) {
FA.Event.SetDefaultEndTime(executionContext);
} else {
alert("enddate was null");
}
}
// Activates onchange events
this.formContext.getAttribute("startdate").addOnChange(FA.Event.SetEndDate);
this.formContext.getAttribute("course_id").addOnChange(FA.Event.SetEndDate);
},
SetDefaultStartTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
startDate.setHours(9, 30, 0);
formContext.getAttribute("startdate").setValue(startDate);
},
SetDefaultEndTime: function (executionContext) {
var formContext = executionContext.getFormContext();
var endDate = formContext.getAttribute("enddate").getValue();
endDate.setHours(17, 0, 0);
formContext.getAttribute("enddate").setValue(endDate);
},
SetEndDate: function (executionContext) {
var formContext = executionContext.getFormContext();
var startDate = formContext.getAttribute("startdate").getValue();
var endDate = formContext.getAttribute("enddate").getValue();
if (formContext.getAttribute("course_id").getValue() != null) {
// Get course
var courseId = formContext.getAttribute("course_id").getValue()[0].id;
// Get days
Xrm.WebApi.retrieveRecord("course", courseId, "?$select=days").then(
function success(result) {
console.log("Retrieved values: Days: " + result.days);
// Round days up
var days = Math.ceil(result.days * Math.pow(10, 0)) / Math.pow(10, 0);
console.log("Days rounded up where decimal result: " + days)
var newEndDate = new Date(startDate);
newEndDate.setHours(endDate.getHours(), endDate.getMinutes(), 0);
newEndDate = addDays(newEndDate, farDays);
alert("newenddate: " + newEndDate);
//sets enddate
formContext.getAttribute("enddate").setValue(newEndDate);
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
}
else {
console.log("End date was not calculated.");
}
function addDays(date, days) {
var newDate = new Date(date);
newDate.setDate(date.getDate() + days);
return newDate;
}
}
}