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
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
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.
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.
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();
}
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.
Hope this helps.
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.
André Arnaud de Cal...
292,516
Super User 2025 Season 1
Martin Dráb
231,430
Most Valuable Professional
nmaenpaa
101,156