Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Business rule - use current date !

(0) ShareShare
ReportReport
Posted on by 95


I would like to use a business rule in the Opportunities form to prevent a date field from being filled with a value that is in the past.
What is the best way to do this?


Thank you & Greetings
Patrick

*This post is locked for comments

  • Martin Donnelly Profile Picture
    Martin Donnelly 1,016 on at
    RE: Business rule - use current date !

    to pop a message for old records (assuming the date limit is createdOn not NOW) you might be able to compare the field to the createdOn in a business rule.  I would be careful about using == between datetime values.  It's almost impossible to be true.

  • Patrick Jaehne Profile Picture
    Patrick Jaehne 95 on at
    RE: Business rule - use current date !

    Hello, thanks for the answers.

    I have now created a field in which the current date will be entered. (OnLoad form)

    Now I can check if other date fields are in the past and display an error message.

    Unfortunately the error message is not displayed when opening the form because the script is too late to say so.

  • Verified answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,231 on at
    RE: Business rule - use current date !

    Hi Patric,

    You cant compare date using business rule. Anyway you could use js to validate it.

    Please refer below code for your reference:

    var DateValue = Xrm.Page.getAttribute("fieldlogicalaname").getValue();

           if (DateValue != null) {

               var today = new Date();

               var todayyear = today.getFullYear() + "";

               var todaymonth = (today.getMonth() + 1) + "";

               var todayday = today.getDate() + "";

               var datepublishyear = DateValue.getFullYear() + "";

               var datepublishmonth = (DateValue.getMonth() + 1) + "";

               var datepublishday = DateValue.getDate() + "";

               var todaydate = new Date(todayyear, todaymonth, todayday);

               var datepublish1 = new Date(datepublishyear, datepublishmonth, datepublishday);

               if (datepublish1 > todaydate) {

                   Xrm.Page.getAttribute("fieldlogicalaname").setValue(null);

                   Xrm.Page.getControl("fieldlogicalaname").setNotification("Date should not be greater than today.");

               }

               else {

                   Xrm.Page.getControl("fieldlogicalaname").clearNotification();

               }

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 Moderator on at
    RE: Business rule - use current date !

    Hi,

    It is not possible to reference current date in a business rule. You can use above mentioned javascript if you want to restrict/validate on change.

    If it is okay to validate on create/ update then you can use Real Time Workflow which triggers on create/ update and checks the date with the current execution time. if it is less then Stop the workflow as cancelled and disaply error.

    survivingcrm.com/.../

    Hope this helps.

  • Verified answer
    RE: Business rule - use current date !

    Hi Patrick,

    Best ways is using javascript:

    function DateFieldValueChange(){

    var datefield= Xrm.Page.getAttribute("new_datefield").getValue();

    if(datefield!= Date.now())

    {

    // clear the field value and give error message

    }

    }

    Refer below link for reference:

    community.dynamics.com/.../set-date-as-today-using-business-roles

    Thanks,

    Swetha

    Please mark the answer as verified if it helped you.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans