在我们的云托管 CRM 2011 中,我在机会表上有 3 个字段
- 合同期限(查找)
- 服务开始日期(日期和时间)
- 服务结束日期(日期和时间)
合同条款是对称为合同条款的自定义实体的查找。项目如下:
我想根据另外两个中的条目计算和填充服务关闭日期字段。我的合同期限和服务开始日期的 OnChange 事件的 javascript 代码如下:
function UpdateAgreementCloseDate() {
//debugger;
if (Xrm.Page.getAttribute("po_contractterm").getValue() != null && Xrm.Page.getAttribute("po_agreementstartdate").getValue() != null)
{
//Get Months from agreement term
var ContractTermMonths = 0;
ContractTermMonths = Xrm.Page.getAttribute("po_contractterm").getValue()[0].keyValues.po_months.value;
//Get Start Date
var currentAgreementStartDate;
currentAgreementStartDate = Xrm.Page.getAttribute("po_agreementstartdate").getValue();
//Add contract term monthsto start date
var AgreementCloseDate = getExpirationDate(currentAgreementStartDate, parseFloat(ContractTermMonths));
Xrm.Page.data.entity.attributes.get("po_agreementclosedate").setValue(AgreementCloseDate);
Xrm.Page.data.entity.attributes.get("po_agreementclosedate").setSubmitMode("always"); // Save the Disabled Field
}
}
function getExpirationDate(tempdate, numberofmonths) {
var next_month = tempdate.getMonth() + numberofmonths;
var next_year = tempdate.getFullYear();
if (next_month > 11) {
if (numberofmonths > 11) {
var extrayears = parseInt(next_month / 12);
var remainingmonths = next_month % 12;
next_month = remainingmonths;
next_year = next_year + extrayears;
}
else {
next_month = 0;
next_year++;
}
}
tempdate.setMonth(next_month);
tempdate.setYear(next_year);
return tempdate;
}
当用户填充计算所需的两个值时,代码会按预期运行。但是,如果其中一个字段(在本例中为合同期限)在创建新记录时在 javascript 中分配了默认值,则当用户为服务开始日期提供值时,不会发生计算:
在我的函数中抛出调试器允许我调试 javascript 并且我看到了问题所在。但是,我不明白为什么这是一个问题。
为什么当用户手动填充这两个字段时,这个确切的代码会起作用,但当其中一个字段被自动填充时却不行?该错误告诉我该对象为空,但显然它在任何一种情况下都不为空。为什么当用户手动输入一个值时这会起作用,但当该值通过 javascript 代码预先填充到表单 OnLoad 方法中时却不起作用?
更新
上面抛出异常的部分是访问 po_months 属性 - 调试器告诉我该属性为空:
ContractTermMonths = Xrm.Page.getAttribute("po_contractterm").getValue()[0].keyValues.po_months