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)

CRM 2011 - Contact Form Loads with cannot assign to a function result onchange of a birthdate field

(0) ShareShare
ReportReport
Posted on by 160

Here is the Code that Manipulates the Birthdate and Age, If age is blank I get an error with the field's customized event. I can't seem to figure out why this JavaScript is throwing the "Cannot assign to a function result" error on a Event: onchange, when this error is appearing on form load. Is there a way to Allow the code to have no action if the Birthdate field is blank?

function birthdate_onchange()

   {

   var age = Xrm.Page.getAttribute("new_age");

   var bDay = Xrm.Page.getAttribute("birthdate");

   if (bDay.getValue() != null)

   {

       var now = new Date();

       var thisYear = now.getFullYear();

       var birthYear = bDay.getValue().getFullYear();

       if((bDay.getValue().getMonth()-now.getMonth())>=0)

       {

           if((bDay.getValue().getMonth()-now.getMonth())==0 && (bDay.getValue().getDate()-now.getDate())<=0)

           {

               age.setValue(thisYear-birthYear);

           }

           else

           {

               age.setValue(thisYear-birthYear-1);

           }

       }

       else

       {

           age.setValue(thisYear-birthYear);

       }

   }

   else

   {

       age.getValue()=null;

   }

   }

I would love some feed back on this as I am new to JavaScript, but want to learn this language very much. Can provide more code or elaboration as needed, as this is just a segment of our Script for Contacts.

Thanks,

PGM

*This post is locked for comments

I have the same question (0)
  • Verified answer
    jlattimer Profile Picture
    24,562 on at

    Instead of:

    age.getValue() = null

    try:

    age.setValue(null)

  • Verified answer
    Community Member Profile Picture
    on at

    I am pretty sure you can just do:

    if (bDay)

    {

    }

    This should evaluate if it's null or not and continue

  • PawelGM Profile Picture
    160 on at

    Thank you for your reply. I appreciate the help! I have actually learn quite a bit of CRM from your Blog Jason. So thanks for what you give back to the CRM community! I'm a fan.

    I have another segment of code that is throwing a 'fireonchange' object doesn't support property or method 'fireonchange' now that I have the age issue resolved.

    Here is the code:

    //TODO: could use to be upgraded to 2011 syntax

       if (Xrm.Page.getAttribute("srobo_birthdatepre1900").getValue() != null) {

           crmForm.all.srobo_birthdatepre1900.style.display = 'inline';

           crmForm.all.srobo_birthdatepre1900_c.style.display = 'inline';

           crmForm.all.birthdate.style.display = 'none';

           crmForm.all.birthdate_c.style.display = 'none';

       }

       else {

           crmForm.all.birthdate.style.display = 'inline';

       }

       try {

           //Sets Age

           //TODO: test if this works

           Xrm.Page.getAttribute("birthdate").fireOnChange();

       }

       catch (err1) {

           alert("Contact onLoad Error 1" + err1 + " " + err1.description);

       }

       try {

       }

       catch (err2) {

           alert("Contact onLoad Error 2 " + entity + ": " + err2 + " " + err2.description);

       }

    } //end on load

    This is the section I converted to a friendly 2011 syntax:  

    Xrm.Page.getAttribute("birthdate").fireOnChange();

    But still get this error. Do you see any areas in the segments that code be still throwing the 'fireonchange' error?

    Again thank you for your feedback!

  • PawelGM Profile Picture
    160 on at

    I will test this approach in my dev. Thank You for the feed back, I appreciate it!

  • Verified answer
    Community Member Profile Picture
    on at

    Why don't you just associate the birthdate attribute with the onChange event in the CRM UI (via forms modification)?

    If you want to do it programatically, you don't want to use fireOnChange, you would want to add an onChange event and register it to that...

    msdn.microsoft.com/.../gg334409.aspx

  • PawelGM Profile Picture
    160 on at

    Just an Update to my last Edit of the content:

    For anyone out there needing it.

    It turns out that my 'fireOnChange' errors were being thrown from some old CRM 4.0 versioning. Seems as though

    "crmForm.all.birthdate.style.display = 'inline';" and

    "crm.form.all.FrieOnChange();"

    just had to be rewritten in the more friendly 2011 syntax. They are now

    " Xrm.Page.getControl("birthdate").setVisible(true);" and

    "Xrm.Page.getAttribute("birthdate").fireOnChange();"

    Thanks All for the Help!

  • PawelGM Profile Picture
    160 on at

    Scott, I agree with you, but I'd like to keep it programmatic for the moment, as I am new to the system. Is the onChange event, the equivalent of the fireonchange?  How could I structure that to be 2011 syntax friendly?

    This is the segment of code I want to alter?

     try {

           //Sets Age

           //TODO: test if this works

           Xrm.Page.getAttribute("birthdate").fireOnChange();

       }

    Here is the whole code below:

    if (Xrm.Page.getAttribute("srobo_birthdatepre1900").getValue(null)) {

           crmForm.all.srobo_birthdatepre1900.style.display = 'inline';

           crmForm.all.srobo_birthdatepre1900_c.style.display = 'inline';

           crmForm.all.birthdate.style.display = 'none';

           crmForm.all.birthdate_c.style.display = 'none';

       }

       else {

           Xrm.Page.getControl("birthdate").setVisible(true);

       }

       try {

           //Sets Age

           //TODO: test if this works

           Xrm.Page.getAttribute("birthdate").fireOnChange();

       }

       catch (err1) {

           alert("Contact onLoad Error 1" + err1 + " " + err1.description);

       }

       try {

       }

       catch (err2) {

           alert("Contact onLoad Error 2 " + entity + ": " + err2 + " " + err2.description);

       }

    } //end on load

    I am taking over a CRM system that was somewhat fragmented from predecessors, so still trying to clean up without breaking, before I learn what all the pieces do and how they fit in.

  • Suggested answer
    Community Member Profile Picture
    on at

    I would register the function that you want to call when the birthdate is changed in the form_onLoad:

    Xrm.Page.getAttribute("birthdate").addOnChange(this.birthdate_onchange);

    Then, when you call the following, it'll call the above function (which by looking at your code does the data manipulation to the data in the birthdate field.  You can also ensure it contains data by doing something like:

    var bd = Xrm.Page.getAttribute("birthdate");

    if (bd)

    {

     Xrm.Page.getAttribute("birthdate").fireOnChange();

    }

    Also, since you are registering the onchange event in the form load, you won't necessarily need to call a fireOnChange (unless you need to) because everytime it changes it will call that function (birthdate_onchange).

  • PawelGM Profile Picture
    160 on at

    Will "Xrm.Page.getAttribute("birthdate").addOnChange(this.birthdate_onchange);"

    take place of the "Xrm.Page.getAttribute("birthdate").fireOnChange();" In this segment:

    try {

           //Sets Age

           //TODO: test if this works

           Xrm.Page.getAttribute("birthdate").fireOnChange();

       }

    Or will "Xrm.Page.getAttribute("birthdate").addOnChange(this.birthdate_onchange);" just be below the

    "Function Form_onLoad(){" at the beginning of the script?

  • Community Member Profile Picture
    on at

    You need to call a javascript function when the form is loaded to register the onchange action to the birthdate attribute:

    Xrm.Page.getAttribute("birthdate").addOnChange(this.birthdate_onchange);

    Then, if you need to manually call the onchange event you would use this elsewhere:

    Xrm.Page.getAttribute("birthdate").fireOnChange();

    Because after you register it in the form load, everytime the field changes it'll be called - no need to call the fireOnChange event unless if for some reason you want it to run manually (e.g., everytime the form saves regardless if the field was changed).

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