Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Restrict on selecting dates

Posted on by 80

Hello,

We have 4 DateTime fields on the form which are used to fill different sale completion dates. Let's say fields are Sale1, Sale2, Sale3, Sale4. We do not want our associates to select "future date" as a sale date. Let's say if today's date is 08/29/2021, the associate should not be able to select anything in future. All the dates in the future should be disabled(greyed out). I know we can validate the date and throw notification that future date cant be selected. I like to find out if there is a way we can grey out all future dates for the field. This we should be doing for all the four fields. Any help on this will be appreciated

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Disable future date

    Thanks

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Disable future date

    Thanks

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Disable future date

    This is perfect. Thank you

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Disable future date

    from my previous code change this line

    var dateFieldName = "new_date1";

    to

    var dateFieldName = executionContext.getEventSource().getName();

    in this way the code can get the attribute name dynamically. Of course the function needs to be attached to the 4 field onchange events and the "Pass execution context as first parameter" checked

    hope it helps

  • Vishnu Kumar9 Profile Picture
    Vishnu Kumar9 80 on at
    RE: Disable future date

    Hello Guido,

    Thanks for responding. Yeah seems like there is no easier way to disable those fields. I have similar query to show alert when they select future date but not to restrict future dates completely

  • Suggested answer
    Nya Profile Picture
    Nya 29,056 on at
    RE: Disable future date

    Hi,

    Please refer to the following issue in the Community of Power Apps:

    Solved: Disable the future dates in datepicker - Power Platform Community (microsoft.com)

    It cannot be greyed out.

    But an OnChange event with JavaScript on the date field could be a wise choice.

    For example, the Birthday field in Contact form should not be a future date. 

    If a future date is selected for the Birthday field, there will be an alert and the field will be empty.

    The code snippet is something like:

    var startDate = Xrm.Page.getAttribute('birthdate').getValue();
    
    var todayDate = new Date(); 
    todayDate.setHours(0,0,0);
    
    if (startDate > todayDate) 
    {
    alert("DOB is greater than today's date");
    Xrm.Page.getAttribute('birthdate').setValue(null);
    }

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Disable future date

    it's not possible to gray out future date but you can attach a javascript function on the onchange event of the datetime field. For example:

    function CheckDate(executionContext) {
    var formContext = executionContext.getFormContext();
    var dateFieldName = "new_date1";
    var fieldValue = formContext.getAttribute(dateFieldName).getValue();
    if (fieldValue != null) {
    	var fieldValueOnlyDate = fieldValue.setHours(0,0,0,0);
    	var today = new Date();
    	today.setHours(0,0,0,0);
    	if (fieldValueOnlyDate > today) {
    		var alertMessage = { text: "Date can't be in the future" };
    		Xrm.Navigation.openAlertDialog(alertMessage);
    		formContext.getAttribute(dateFieldName).setValue(today);
    	}
    }   
    }

    note: code has not been tested and if you plan to run the same code on multiple fields it should be adapted

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans