クラウドでホストされている CRM 2011 では、商談フォームに 3 つのフィールドがあります。
- 契約期間(参照)
- サービス開始日(日時)
- サービス終了日(日時)
契約条件は、契約条件と呼ばれるカスタム エンティティのルックアップです。項目は次のとおりです。
他の 2 つのエントリに基づいて、Service Close Date フィールドを計算して入力したいと考えています。契約期間とサービス開始日の 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;
}
ユーザーが計算に必要な 2 つの値を入力すると、コードは期待どおりに実行されます。ただし、フィールドの 1 つ (この場合は契約期間) に新しいレコードの作成時に JavaScript で割り当てられたデフォルト値がある場合、ユーザーがサービス開始日に値を指定しても計算は行われません。
関数でデバッガーをスローすると、javascript をデバッグできるようになり、問題が何であるかがわかります。しかし、なぜそれが問題なのか理解できません。
ユーザーが両方のフィールドを手動で入力すると、この正確なコードが機能するのに、一方が自動入力された場合は機能しないのはなぜですか? このエラーは、オブジェクトが null であることを示していますが、どちらの場合も明らかに null ではありません。ユーザーが値を手動で入力したときにこれが機能するのはなぜですか?
アップデート
上記の例外をスローする行の部分は、po_months プロパティにアクセスしています。デバッガーは、プロパティが null であることを通知します。
ContractTermMonths = Xrm.Page.getAttribute("po_contractterm").getValue()[0].keyValues.po_months