How to show # of months since another field on a form - Please help!

Proposed Answer How to show # of months since another field on a form - Please help!

  • Wednesday, June 01, 2011 12:40 PM
     
     

    I’m trying to create a navchar attribute (which m-ay be part of the problem) that will give me months since another attribute. So I’ve got a datettime attribute named new_dateofformalclaim and I’d like to have the attribute named new_monthssinceformalclaim show the months that have elapsed since the formal claim. This is what I’ve done:

    On the onLoad for the form I’ve loaded this:
    var CRM_FORM_TYPE_CREATE = 1;

    var CRM_FORM_TYPE_UPDATE = 2;

    crmForm.all.new_monthssinceformalclaim.Disabled = true;

    switch (crmForm.FormType)

    {

    case CRM_FORM_TYPE_CREATE:

    break;

    case CRM_FORM_TYPE_UPDATE:

    if (crmForm.all.new_dateofformalclaim.DataValue != null) {

    crmForm.all.new_dateofformalclaim.FireOnChange();

    }

    break;

    }
    Then on the onSave for the form I’ve:


    crmForm.all.new_monthssinceformalclaim.ForceSubmit = true;

    Then I’ve added this code to the onChange Event for Months since Formal Claim attribute on the form:
    var currentdate = new Date();

    millisecondsfromdateofformalclaim = crmForm.all.new_dateofformalclaim.DataValue – currentdate;

    crmForm.all.new_monthssinceformalclaim.DataValue = Math.round(millisecondstoshipdate/(1000*60*60*24));

    What am I doing wrong?

    Thanks in advance!
    Jenn

All Replies

  • Wednesday, June 01, 2011 1:07 PM
    Moderator
     
     

    Hello Jenn,

    What kind of issue do you get? Try to use following code:

    crmForm.all.new_monthssinceformalclaim.DataValue = Math.round(millisecondstoshipdate/(1000*60*60*24)) + '';


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
  • Wednesday, June 01, 2011 1:46 PM
     
     

    Hi there,

    The following script might help to count the number of months:

    countMonths = function(startDate, endDate) {
      var startYear = startDate.getYear();
      var endYear = endDate.getYear();

      var yearDiff = endYear - startYear;
     
      var startMonth = startDate.getMonth();
      var endMonth = endDate.getMonth();

      var monthDiff = (endMonth - startMonth) + (yearDiff * 12);

      return monthDiff;
    }

    var currentdate = new Date();
    var startDate = new Date(crmForm.all.new_dateofformalclaim.DataValue);
    var endDate = currentdate;

    crmForm.all.new_monthssinceformalclaim.DataValue = countMonths(startDate, endDate);

    Hope that helps.


    Kind Regards, Paul.
  • Wednesday, June 01, 2011 2:02 PM
     
     

    The issue is the box remains empty - no data is entered. 

    I tried the code - I replaced the last line of the onChange code for the attributes event on the form that you provided and still nothing.

    Should I have removed all the code I had in the onChange event for the attribute on the form?  Also I left onSave & onLoad code for the form - should I have removed it?

    Thanks!

    Jenn

  • Wednesday, June 01, 2011 2:05 PM
     
     

    Thanks Paul

    Still nothing in the months since formal claim.  I replaced the code I had in the onChange event for the "months since formal claim" attribute with the code you provided but left everything else I had in the onLoad & OnSave for the form - should I have removed it? 

    Thanks!

    Jenn

     

  • Wednesday, June 01, 2011 2:10 PM
     
     Proposed Answer

    Hi Jenn,

    If you put the following script into the onChange event of the new_dateofformalclaim field, does anything happen?

    countMonths = function(startDate, endDate) {
      var startYear = startDate.getYear();
      var endYear = endDate.getYear();

      var yearDiff = endYear - startYear;
     
      var startMonth = startDate.getMonth();
      var endMonth = endDate.getMonth();

      var monthDiff = (endMonth - startMonth) + (yearDiff * 12);

      return monthDiff;
    }

    try {

    var currentdate = new Date();
    var startDate = new Date(crmForm.all.new_dateofformalclaim.DataValue);
    var endDate = currentdate;

    crmForm.all.new_monthssinceformalclaim.DataValue = countMonths(startDate, endDate);

    }

    catch(ex) {

      alert("Error: " + ex.description);

    }

    In theory this should populate the Months since Formal Claim field whenever the Date of Formal Claim field is changed - so hopefully should see either the logic take place, or an error prompt.

    If this works for the onChange event, could then think to moving this same logic onto the onSave event to calculate each time the screen is saved.



    Kind Regards, Paul.

    • Proposed As Answer by P McQ Wednesday, June 08, 2011 1:10 PM
    •  
  • Wednesday, June 01, 2011 2:24 PM
     
     

    Paul,

    Tried what you post & literally copy & pasted it into the onChange event.  Still nothing - no error, no data - but the box looks like it's been inactivated - does that help?

    Thx!

    Jenn

  • Wednesday, June 01, 2011 2:41 PM
     
     

    Might be worth checking that the Date of Formal Claim onChange event is activated - when you go to edit the script of the event there is a box for 'Event is Enabled', make sure this is ticked otherwise none of the scripting will run when the onChange event fires.

    The other possibility is that the script is causing a Javascript error but IE is not readily displaying the error - when changing the Date of Formal Claim field to a different date, check whether a yellow and black icon appears in the very bottom left of IE as this would indicate that the script is raising a Javascript error.

    Which box looks inactive?


    Kind Regards, Paul.
  • Wednesday, June 01, 2011 3:00 PM
     
     

    Paul,

    Yes I've clicked the box active upon adding the original code. I'm not seeing a yellow black icon either.  The box that looks inactive is the Months Since Since Formal Claim attribute on the form - the ouline is gray rather than black??  I really appreciate the help you've given this morning.  I unfortunately have to leave the office - I'm going on vacation till Monday!  I'd like to see if we could pick up then?  It would be greatly appreciated!

    Thanks again so much for your solutions!

    Jenn

     

  • Wednesday, June 01, 2011 3:20 PM
     
     

    Sounds like the Months Since Formal Claim field is disabled on the form - however this should not be a problem as our logic is looking to calculate an automatic value for this field, and so the field probably should be read-only for the user.

    Will pick up on Monday when your back - however looks like it might be worth checking that the script is definately going on the other Date of Formal Claim field's onChange (and so not the Months Since Formal Claim onChange), and that the entity is Published after we have saved the changes to the Form.  Bit basic suggestions, but otherwise the onChange script should definately be doing something when a user updates the Date of Formal Claim field!


    Kind Regards, Paul.