如何用新值更新executionContext?是否可以从executionContext中获取新值?JS D365



新日期不会覆盖executionContext日期重试

嗨!我有一个javascript,它根据开始日期计算结束日期。该函数为开始日期字段触发onChange。但在此之前,还会为开始日期和结束日期设置默认时间。只有当它是一个创建表单时才会发生这种情况。

这里的问题是,当打开创建表单时,我从executionContext中检索开始和结束日期,并为这两个字段设置默认时间。然后运行onChange函数,我们再次从executionContext中检索开始日期和结束日期字段,根据检索到的开始日期计算新的结束日期,并确保在新的结束日设置上一个结束日期时间。我得到的时间是executionContext捕获的第一个时间,而不是我更新到的默认时间。

有没有办法让我获得新的值(默认时间(,并通过executionContext在onChange函数中获取它,而不必创建一个新的单独的js?executionContext何时更新,因为表单在获得onChange值之前确实获得了一毫秒的默认结束时间。

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;
}
}
}

我通过使用开始日期和结束日期的全局变量来解决这个问题,我从executionContext中为其设置了值。如果函数setDefaultEndTime或SetDetfaultStartTime运行,则会更新要使用的SetEndDate函数的变量,否则将使用原始值。

我没有发现任何关于更新executionContext之类的内容。

相关内容

  • 没有找到相关文章

最新更新