web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Simple JavaScript... how to set value of a field with a variable

(0) ShareShare
ReportReport
Posted on by

In this post we have a function for calculate the age of a contact based on the birthdate field.  The poster updates the label of the birthdate field with the "age" variable.  I want to update the "age" field with the "age" variable.  I don't know JavaScript, but gave a go at deciphering what's there thought it might be:

    Xrm.Page.getAttribute("age").setValue(age);

Which doesn't work.  I gave it a try. ;-)

In this case age is the field name and the variable name.

*This post is locked for comments

I have the same question (0)
  • Royal King Profile Picture
    27,686 on at

    is age field read-only in the crm form? if so you have to set submit mode in order to save the value

    Xrm.Page.getAttribute("age").setSubmitMode("always");

  • Cyclefitness Profile Picture
    on at

    yes, it's read only.  I updated and now get this error:

    TypeError: Unable to get property 'setSubmitMode' of undefined or null reference at BirthDateFunction

  • Royal King Profile Picture
    27,686 on at

    can you post complete code that you have?

  • Cyclefitness Profile Picture
    on at

    function onLoad() {

    BirthDateFunction();

    }

    function onChange_birthdate() {

    BirthDateFunction();

    }

    function BirthDateFunction() {

    var birthDate = Xrm.Page.getAttribute("birthdate").getValue();

    var birthDateControl = Xrm.Page.ui.controls.get("birthdate");

    if(birthDate != "" && birthDate != null) {

      var age = CalculateAge(birthDate);

      var newLabel = "DOB (Age: " + age + ")";

    } else {

      var newLabel = "DOB";

    }

    birthDateControl.setLabel(newLabel);

    // added by Deb on 11-4-15 to set value of age

        Xrm.Page.getAttribute("age").setSubmitMode("always");

    //

    }

    function CalculateAge(birthDate) {

      birthDate = new Date(birthDate);

      var todayDate = new Date();

      var years = (todayDate.getFullYear() - birthDate.getFullYear());

      if (todayDate.getMonth() < birthDate.getMonth() ||

          todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() < birthDate.getDate()) {

          years--;

      }

    if(todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() == birthDate.getDate()) {

      alert("Say happy birthday!");

    }

      return years;

    }

  • Verified answer
    Royal King Profile Picture
    27,686 on at

    Are you sure age field is in the form ? It seems this field does not exists in the form and you are trying to set value for it... If this field is not in the form add it  to the form and place below script in your file

    function onLoad() {

       BirthDateFunction();

    }

    function onChange_birthdate() {

       BirthDateFunction();

    }

    function BirthDateFunction() {

       var birthDate = Xrm.Page.getAttribute("birthdate").getValue();

       var birthDateControl = Xrm.Page.ui.controls.get("birthdate");

       if (birthDate != "" && birthDate != null) {

           var age = CalculateAge(birthDate);

           var newLabel = "DOB (Age: " + age + ")";

       } else {

           var newLabel = "DOB";

       }

       birthDateControl.setLabel(newLabel);

       // added by Deb on 11-4-15 to set value of age

       Xrm.Page.getAttribute("age").setValue(age);

       Xrm.Page.getAttribute("age").setSubmitMode("always");

       //

    }

    function CalculateAge(birthDate) {

       birthDate = new Date(birthDate);

       var todayDate = new Date();

       var years = (todayDate.getFullYear() - birthDate.getFullYear());

       if (todayDate.getMonth() < birthDate.getMonth() ||

           todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() < birthDate.getDate()) {

           years--;

       }

       if (todayDate.getMonth() == birthDate.getMonth() && todayDate.getDate() == birthDate.getDate()) {

           alert("Say happy birthday!");

       }

       return years;

    }

  • Cyclefitness Profile Picture
    on at

    I forgot to add the "new-" prefix to age.  Once I did that and copied and pasted your code, it works perfectly.

    Thanks very much, Chitra!

  • Cyclefitness Profile Picture
    on at

    What would need to change for me to use in a QuickCreate Form where I wouldn't display the age field?  Would I have to put it on the form and hide it or can I leave it off the form entirely?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans