locked
Date cannot be less than today RRS feed

  • Question

  • Folks:

    I need help with a date validation in Dynamics CRM 2011. I have a field as 'Next Contact Date' which accepts date, but I don't want users to enter date less than today. I believe this can be achieved only through JavaScript, could somebody help me with this?

    Thanks !

    Tuesday, October 21, 2014 5:47 PM

Answers

  • Hi,

    Following method will do the required. Let us know if you face any issues.

    function ValidateNextContactDate() {
        var contactDate = Xrm.Page.getAttribute('new_nextcontactdate').getValue();
        contactDate.setHours(0, 0, 0, 0);
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        if (contactDate < today) {
            alert("Your Error Message");
            event.returnValue = false;
        }
    }


    Vikranth Pandiri Blog: http://howto-mscrm.blogspot.com Twitter: @TweetVikranth "Please Mark it as answer if it helps in resolving your query"

    • Proposed as answer by Vikranth Pandiri Tuesday, October 21, 2014 11:50 PM
    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Tuesday, October 21, 2014 11:50 PM
  • Vikranth's answer is correct except for the line

    event.returnValue = false;

    that is CRM 4.0 code and is not supported inside CRM 2011. instead of that line you can clear the nextcontactdate field so the user needs to select another date.

    Xrm.Page.getAttribute('new_nextcontactdate').setValue(null);

     

    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Wednesday, October 22, 2014 3:26 AM
  • Folks:

    Thanks for all the answers but I'm getting this error:

    There was an error with this field's customized event.
    Field: new_nextcontactdate
    Event: onchange
    Error: Unable to get property 'setHours' of undefined or null reference


    you need to check that the value is not null before set the hours:

    var contactDate = Xrm.Page.getAttribute('new_nextcontactdate').getValue();
    if (contactDate != null) {
        contactDate.setHours(0, 0, 0, 0);
        // rest of the code


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Wednesday, October 22, 2014 4:58 PM

All replies

  • Hi,

    Following method will do the required. Let us know if you face any issues.

    function ValidateNextContactDate() {
        var contactDate = Xrm.Page.getAttribute('new_nextcontactdate').getValue();
        contactDate.setHours(0, 0, 0, 0);
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        if (contactDate < today) {
            alert("Your Error Message");
            event.returnValue = false;
        }
    }


    Vikranth Pandiri Blog: http://howto-mscrm.blogspot.com Twitter: @TweetVikranth "Please Mark it as answer if it helps in resolving your query"

    • Proposed as answer by Vikranth Pandiri Tuesday, October 21, 2014 11:50 PM
    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Tuesday, October 21, 2014 11:50 PM
  • Vikranth's answer is correct except for the line

    event.returnValue = false;

    that is CRM 4.0 code and is not supported inside CRM 2011. instead of that line you can clear the nextcontactdate field so the user needs to select another date.

    Xrm.Page.getAttribute('new_nextcontactdate').setValue(null);

     

    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Wednesday, October 22, 2014 3:26 AM
  • Hi,

    If you want to perform validation when user performs save operation than make sure " The pass execution context as first parameter" checkbox is selected.

    function Form_onsave(executionObj)
    {
        var shouldSave = true;
    
        if (shouldSave)
        {
         var contactDate = Xrm.Page.getAttribute('new_nextcontactdate').getValue();
        contactDate.setHours(0, 0, 0, 0);
        var today = new Date();
        today.setHours(0, 0, 0, 0);
        if (contactDate < today) {
        
            alert("Date can not be less than today");
    
            executionObj.getEventArgs().preventDefault();
        }
           
        }
    }
    

    if you just want the validation as soon as user changes value of the date field, than go with guido's suggestion. You can show alert message and clear the value. 

    Thanks!

    Kalim Khan

    Wednesday, October 22, 2014 6:27 AM
  • Folks:

    Thanks for all the answers but I'm getting this error:

    There was an error with this field's customized event.
    Field: new_nextcontactdate
    Event: onchange
    Error: Unable to get property 'setHours' of undefined or null reference


    • Edited by sqldba20 Wednesday, October 22, 2014 3:45 PM
    Wednesday, October 22, 2014 3:45 PM
  • Are you using the same code ?? you should not face this issue because this is a standard javascript date method ?? Share your code here

    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our 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, October 22, 2014 3:57 PM
    Moderator
  • I'll agree with that - it's standard JavaScript code and what's written there is sound. I'm doing something very similar on a few system I look after and the syntax is identical.

    You may just want to check that you've not accidentally removed a curly brace from a previous script. To rule this out, try replacing the code within the function with a single 'alert' statement - if this works, it would rule out any other code in the library; if you still get the error, then there's an issue elsewhere in the code.

    Wednesday, October 22, 2014 4:34 PM
  • Folks:

    Thanks for all the answers but I'm getting this error:

    There was an error with this field's customized event.
    Field: new_nextcontactdate
    Event: onchange
    Error: Unable to get property 'setHours' of undefined or null reference


    you need to check that the value is not null before set the hours:

    var contactDate = Xrm.Page.getAttribute('new_nextcontactdate').getValue();
    if (contactDate != null) {
        contactDate.setHours(0, 0, 0, 0);
        // rest of the code


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by sqldba20 Wednesday, October 22, 2014 5:11 PM
    Wednesday, October 22, 2014 4:58 PM