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

  • Question

  • 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

    Tuesday, May 20, 2014 4:05 PM

All replies

  • Hi PGM,

    Your problem is this line:

    age.getValue()=null;

    If you want to set some value to a field you should use the following syntax. 

    age.setValue(valueYouWantToSet);

    Thanks

    Sachith




    Sachith Chandrasiri

    Wednesday, May 28, 2014 4:40 AM