Note that there are some explanatory texts on larger screens.

plurals
  1. POCRM 2011 javascript error - object is undefined, when clearly it is defined
    text
    copied!<p>In our cloud-hosted CRM 2011 I have 3 fields on an opportunity form</p> <ul> <li>Contract Term (Lookup)</li> <li>Service Start Date (Date And Time)</li> <li>Service Close Date (Date And Time)</li> </ul> <p><img src="https://i.stack.imgur.com/OcMhI.png" alt="Default values of a new form"></p> <p>Contract Term is a Lookup on a custom entity called Contract Terms. The items are listed below:</p> <p><img src="https://i.stack.imgur.com/Rh1fQ.png" alt="All Contract Terms records"></p> <p>I want to calculate and populate the Service Close Date field based on entries in the other two. My javascript code for the OnChange event of Contract Term and Service Start Date is as follows:</p> <pre><code>function UpdateAgreementCloseDate() { //debugger; if (Xrm.Page.getAttribute("po_contractterm").getValue() != null &amp;&amp; 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 &gt; 11) { if (numberofmonths &gt; 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; } </code></pre> <p>When the user populates the two values that are required for the calculation, the code runs as expected. But if one of the fields (in this case Contract Term) has a default value assigned in javascript on the creation of a new record, the calculation doesn't occur when the user gives a value to Service Start Date:</p> <p><img src="https://i.stack.imgur.com/VXZSA.png" alt="Calculation did not happen"></p> <p>Throwing debugger in my function allows me to debug the javascript and I see what the problem is. But, I don't understand why it's a problem.</p> <p><img src="https://i.stack.imgur.com/e6eDO.png" alt="Exception"></p> <p>Why would this exact code work when the user manually populates both fields, but not when one of them is auto populated? The error is telling me that the object is null, but clearly it is not null in either case. Why would this work when the user manually enters a value, but not when the value is prepopulated in the forms OnLoad method by javascript code?</p> <p><strong>UPDATE</strong></p> <p>The part of the line that throws the exception above is accessing the po_months property - debugger tells me the property is null: </p> <pre><code>ContractTermMonths = Xrm.Page.getAttribute("po_contractterm").getValue()[0].keyValues.po_months </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload