locked
Elapsed Time Error RRS feed

  • Question

  • I am trying to calculate elapsed time from a date field to today's date.  Code works if the field contains data, but throws an error if null.  Error is "Unable to get property "getFullYear" of undefined or null reference. Here is the code:

    function Form_onload()
    {

    var today = new Date();
    var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue();
    var y1 = today.getFullYear();
    var y2 = RSD.getFullYear();
    var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

    if(RSD != null)
       {
          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }

    }

    Any help is appreciated!


    Ken Compter

    Tuesday, March 25, 2014 3:08 PM

Answers

  • Thanks for your reply.  Still getting an error (but a different one).  Error: "The value of the property 'Form_onload' is null or undefined, not a Function object.  Here is what I tried:

    function Form_onload()
    {
    if(Xrm.Page.getAttribute("new_relationshipstartdate").getValue() !=null)
       {
         var today = new Date();
         var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue());
         var y1 = today.getFullYear();
         var y2 = RSD.getFullYear();
         var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }
    }

    I am guessing it is a syntax error?


    Ken Compter

    Hi Ken,

    There is a syntax error in your code. One extra ) on 6th line. Correct code is below

    function Form_onload()
    {
    if(Xrm.Page.getAttribute("new_relationshipstartdate").getValue() !=null)
       {
         var today = new Date();
         var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue();
         var y1 = today.getFullYear();
         var y2 = RSD.getFullYear();
         var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }
    }


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    • Proposed as answer by HIMBAPModerator Thursday, March 27, 2014 5:42 AM
    • Marked as answer by kcompter Thursday, March 27, 2014 12:33 PM
    Thursday, March 27, 2014 5:03 AM

All replies

  • Check if the RSD is a Date at all before trying to perform any function on it.

    if (RSD == "Invalid Date")
    {
                 
    }

    • Proposed as answer by Ahmad Pirani Wednesday, March 26, 2014 1:25 AM
    • Unproposed as answer by kcompter Wednesday, March 26, 2014 4:45 PM
    Tuesday, March 25, 2014 3:13 PM
  • Hello,

    Why don't you simply apply condition to check if value is not equal to null and only then capture value

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

    var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue();


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, March 26, 2014 12:30 AM
    Moderator
  • Thanks for your reply.  Still getting an error (but a different one).  Error: "The value of the property 'Form_onload' is null or undefined, not a Function object.  Here is what I tried:

    function Form_onload()
    {
    if(Xrm.Page.getAttribute("new_relationshipstartdate").getValue() !=null)
       {
         var today = new Date();
         var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue());
         var y1 = today.getFullYear();
         var y2 = RSD.getFullYear();
         var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }
    }

    I am guessing it is a syntax error?


    Ken Compter

    Wednesday, March 26, 2014 12:26 PM
  • Thanks for your reply.  Still getting an error (but a different one).  Error: "The value of the property 'Form_onload' is null or undefined, not a Function object.  Here is what I tried:

    function Form_onload()
    {
    if(Xrm.Page.getAttribute("new_relationshipstartdate").getValue() !=null)
       {
         var today = new Date();
         var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue());
         var y1 = today.getFullYear();
         var y2 = RSD.getFullYear();
         var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }
    }

    I am guessing it is a syntax error?


    Ken Compter

    Hi Ken,

    There is a syntax error in your code. One extra ) on 6th line. Correct code is below

    function Form_onload()
    {
    if(Xrm.Page.getAttribute("new_relationshipstartdate").getValue() !=null)
       {
         var today = new Date();
         var RSD = Xrm.Page.getAttribute("new_relationshipstartdate").getValue();
         var y1 = today.getFullYear();
         var y2 = RSD.getFullYear();
         var monthDiff = (y1- y2) + (today.getMonth() - RSD.getMonth())/12;

          Xrm.Page.getAttribute("synact_tenure").setValue(monthDiff);
          Xrm.Page.getAttribute("synact_tenure").setSubmitMode("always");
       }
    }


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful.
    Mubasher Sharif
    Check out my about.me profile!
    http://mubashersharif.blogspot.com
    Linked-In Profile
    Follow me on Twitter!

    • Proposed as answer by HIMBAPModerator Thursday, March 27, 2014 5:42 AM
    • Marked as answer by kcompter Thursday, March 27, 2014 12:33 PM
    Thursday, March 27, 2014 5:03 AM
  • That was it, thank you for the help!  Can you take a look at this one as well?  Same error (not a function object).

    //Update Potential Customer with selection
    function synact_potentialaccountid_onchange()
    {
    var PT = Xrm.Page.getAttribute("synact_potentialaccountid");
    var PC = Xrm.Page.getAttribute("customerid");

    if(PT.getValue()!=null)
    {
       PC.setValue(PT);
       Xrm.Page.data.entity.save();
    }
    }


    Ken Compter

    Thursday, March 27, 2014 12:37 PM