(Updated) CRM 2011: Date field not set when manually entered and a button clicked. RRS feed

  • Question

  • I'm running into an oddity with the a date field on a CRM form that I'd guess would be pretty common and hoping others have addressed. 

    I have a check in JavaScript that is pulling the value from a date field on a CRM form after a button-click. The JavaScript logic executes from a ribbon button-click event, which executes validations to determine if the record can be  saved. If the validations fail, a message is popup to the user and the record is not saved.

    However, when I test this by manually entering a date (not selecting it from the calendar control but just typing it in by hand) and then immediately clicking the button without changing focus away from the date field, the following code does NOT pull the updated value.  It is only pulling the original value or if the date field has lost focus, it will pull the updated value correctly.  However, if I use the calendar control, it works fine and as expected. The difference seems to be that when you manually enter a date in the calendar control's text field, the field needs to lose focus before it actually accepts the date.  That makes some sense because it has to validate what you entered is a valid date. So, since the button is on the ribbon (another IFRAME I believe), JavaScript does not see the new date that has been entered until the calendar control validates it and accepts it. 

    My problem is that the JavaScript is used to validate the record before allowing it to be saved, so I cannot save it first, then execute the validations, as that would mess up the whole point here, which is to stop the user from saving if there are validation issues. I need a way for the JavaScript to valid a manually entered date without having to change focus elsewhere on the page. 

    Here's the code I am using, which is basic and common JavaScript to pull the value from a field.

    var vdhisuspendedreason = Xrm.Page.getAttribute("dhi_suspendedreason").getValue();

    I have also used the following, with the same results.


    Any suggestions on how to get the manually entered date from the calendar control without having to save the record or change focus? This seems a bit odd but sort of makes sense that the SDK couldn't access the field value unless a lost-focus event was fired, which doesn't occur if you only click a button on the ribbon, as the ribbon is contained within it's own IFRAME. 

    UPDATE: I have figured out that if I add a plugin and do the validations there, the plugin always knows of the updated value when manually entered.  Apparently CRM is doing something to store the values before calling the plugin and doing so forces the calendar control to evaluate the manually entered date.  But this approach is more of a challenge because now I have to keep the plugin from saving the record and return the message to the user.  I was able to do that by returning a JSON formatted error messages to the CRM Form and use JavaScript to display a modal popup message and block the save from occurring.  But it seems like a lot of work to accomplish this.  So I'm hoping someone has ran into this before and might provide a less-complicated solution.  All I need to do is have the calendar control evaluate the entered text when the button is clicked.  Then the JavaScript will work correctly and the plugin is not needed.

    Best regards,


    Jon Gregory Rothlander

    Tuesday, June 30, 2015 5:14 PM