Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:42:14+00:00 2026-06-13T15:42:14+00:00

In our cloud-hosted CRM 2011 I have 3 fields on an opportunity form Contract

  • 0

In our cloud-hosted CRM 2011 I have 3 fields on an opportunity form

  • Contract Term (Lookup)
  • Service Start Date (Date And Time)
  • Service Close Date (Date And Time)

Default values of a new form

Contract Term is a Lookup on a custom entity called Contract Terms. The items are listed below:

All Contract Terms records

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:

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

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:

Calculation did not happen

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.

Exception

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?

UPDATE

The part of the line that throws the exception above is accessing the po_months property – debugger tells me the property is null:

ContractTermMonths = Xrm.Page.getAttribute("po_contractterm").getValue()[0].keyValues.po_months
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:42:16+00:00Added an answer on June 13, 2026 at 3:42 pm

    I think I understand this problem now.

    In short: You are assuming that a lookup field will give you access to the fields of the lookup’ed record. This is incorrect.

    To expand on that, lets start with what Crm actually gives you in a lookup field.

    If you have a look at the MSDN here. We can see that when getValue() is performed on a lookup attribute we are returned an array with three string properties.

    entityType
    String: the name of the entity displayed in the lookup

    id
    String: The string representation of the GUID value for the record displayed in the lookup.

    name
    String: The text representing the record to be displayed in in the lookup.

    Things to note:

    There is no keyValues property (I’m not sure where you have got this from – please let me know if I’m missing something here).

    Apart from the name field the we don’t have any other record data. E.g: If this was a lookup to a contact we could tell its fullname (from the name property) but not its address or phone number.

    So, to summarise the reason it fails when Crm sets the lookup is because it only provides the information from above, it will not populate keyValues or any of the fields from the lookup’ed record, e.g. po_months.

    I’m guessing the reason it works when you set the lookup, is because you are creating the keyValues property somewhere else in code.

    If you want information from a record related via a lookup, you have to perform a webservice call to get that information – using the id from the lookup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app hosted in a cloud environment which can be expanded
We have our application hosted on Google app engine for Java and we have
I'm trouble porting a static library for our cloud database service to ARC. I've
We have cloud-hosted (RackSpace cloud) Ruby and Java apps that will interact as follows:
I have already knowledge of windows azure and worked on cloud service,storage & WASD,
I have seen on our cloud servers, there is some text printed on the
We have a cloud based SaaS application and many of our customers (school systems)
We're using EngineYard Cloud to deploy our Ruby on Rails application. We are running
How can we move our existing ASP .Net 2.0 application to the cloud platform
We're investigating the cloud for a couple of solutions and for our core product.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.