SBX - Search With Button

SBX - Forum Post Title

Disable or doesn't allow to select past date

Microsoft Dynamics CRM Forum

Manoj Dynamics 365 asked a question on 5 Feb 2019 6:24 AM

Question Status

Suggested Answer

Hello Sir,

My question is , How to Prevent past date selection from  date field if the date field have +3 future days followup ?

means i.e. (05/02/2019)+3days = 08/02/2019 means date field doesn't allow to select 07/02/2019 or past dates

please help

Reply
Wei Jie Fun responded on 5 Feb 2019 6:32 AM
Suggested Answer

Hi,

you can use javascript to do the calculation between days.

if the final result is not 3 days.

use setnotification to show message.

Reply
David Jennaway responded on 5 Feb 2019 6:36 AM
My Badges

You'll need to use a javascript function to do this. This can run on change of the date field, and can throw an error, and clear out the value, if the user selects a date that is in the past.

It is just about possible to do this without code, as a business rule allows you to compare the difference between 2 date fields, and you could probably use a workflow or calculated field to store the current date, but this is quite messy, and in this case I think javascript is simpler

Reply
Manoj Dynamics 365 responded on 5 Feb 2019 6:44 AM

I have only one date field & followup for +3 for same field

Reply
Wei Jie Fun responded on 5 Feb 2019 6:47 AM
Suggested Answer

hi,

you could use current date and compare with the follow up date.

var currentdate=new Date(); //which will return current date and time object

Reply
Suggested Answer

Dynamics 365 calendar control cannot be configured or modified in a supported way to do what you mentioned above.

You could have a hidden field on the form to keep the current date and set its value using a tiny js function on load of the form as shown below

function SetCurrentDate()

{

Xrm.Page.getAttribute("new_todaydate").setValue(Date.now());

}

And you can then have a business rule on the selected date field, and if the selected value is less than the today's date field value, you can use the Set Error on the field. This will prevent the record from being saved and also the red error message on the field will force the user to select valid date.

Hope this helps

Reply
Manoj Dynamics 365 responded on 5 Feb 2019 7:37 AM

But I want only future dates and if you select one  past day from that future date  then it should not be allowed in same date field

Reply
Manoj Dynamics 365 responded on 6 Feb 2019 12:30 AM

How to prevent past date selection from future date ?

Reply
Manoj Dynamics 365 responded on 6 Feb 2019 12:30 AM

How to prevent past date selection from future date , NOT from "Today's Date"

Reply
Suggested Answer

Manoj ,

Try with this -

// Register below method in onchange event 

function ValidatePastDate() {
    var dateFieldVal =  Xrm.Page.getAttribute("new_dateField").getValue(); // Replace Field Name
    var dateFieldValDT = new Date(dateFieldVal);
    var dateFieldValWithFormat = dateFieldValDT.getFullYear() + '/' + (dateFieldValDT.getMonth() + 1) + '/' + dateFieldValDT.getDate(); 
    var todaydate = new Date();
    var todaywithFormat = todaydate.getFullYear() + '/' + (todaydate.getMonth() + 1) + '/' + todaydate.getDate(); 
    if (dateFieldValWithFormat < todaywithFormat) {
        alert("Selected  date less than  today");
        rm.Page.getAttribute("new_dateField").setValue(null);
    }
    else {
        alert("Selected event date greater than today");
    }
}


Reply
Shivaram Santosh responded on 6 Feb 2019 4:48 AM
My Badges
Suggested Answer

Hi Manoj,

You can only write a Javascript function and there you can get the current date + 3 days. Once you have this value, then you need to check what is updated value. If he selects which supposed to restrict, then you can clear the value and Show error message. This is the best option to achieve your requirement.Hope it helps.

Thanks,

Shivaram.

Reply
Manoj Dynamics 365 responded on 9 Feb 2019 2:26 AM

Please share code for the same

Reply
Suggested Answer

Could you please provide more details on your requirement? If you could share screenshot and sample we might be able to help

Reply
Wei Jie Fun responded on 5 Feb 2019 6:32 AM
Suggested Answer

Hi,

you can use javascript to do the calculation between days.

if the final result is not 3 days.

use setnotification to show message.

Reply
Wei Jie Fun responded on 5 Feb 2019 6:47 AM
Suggested Answer

hi,

you could use current date and compare with the follow up date.

var currentdate=new Date(); //which will return current date and time object

Reply
Suggested Answer

Dynamics 365 calendar control cannot be configured or modified in a supported way to do what you mentioned above.

You could have a hidden field on the form to keep the current date and set its value using a tiny js function on load of the form as shown below

function SetCurrentDate()

{

Xrm.Page.getAttribute("new_todaydate").setValue(Date.now());

}

And you can then have a business rule on the selected date field, and if the selected value is less than the today's date field value, you can use the Set Error on the field. This will prevent the record from being saved and also the red error message on the field will force the user to select valid date.

Hope this helps

Reply
Suggested Answer

Manoj ,

Try with this -

// Register below method in onchange event 

function ValidatePastDate() {
    var dateFieldVal =  Xrm.Page.getAttribute("new_dateField").getValue(); // Replace Field Name
    var dateFieldValDT = new Date(dateFieldVal);
    var dateFieldValWithFormat = dateFieldValDT.getFullYear() + '/' + (dateFieldValDT.getMonth() + 1) + '/' + dateFieldValDT.getDate(); 
    var todaydate = new Date();
    var todaywithFormat = todaydate.getFullYear() + '/' + (todaydate.getMonth() + 1) + '/' + todaydate.getDate(); 
    if (dateFieldValWithFormat < todaywithFormat) {
        alert("Selected  date less than  today");
        rm.Page.getAttribute("new_dateField").setValue(null);
    }
    else {
        alert("Selected event date greater than today");
    }
}


Reply
Shivaram Santosh responded on 6 Feb 2019 4:48 AM
My Badges
Suggested Answer

Hi Manoj,

You can only write a Javascript function and there you can get the current date + 3 days. Once you have this value, then you need to check what is updated value. If he selects which supposed to restrict, then you can clear the value and Show error message. This is the best option to achieve your requirement.Hope it helps.

Thanks,

Shivaram.

Reply
Suggested Answer

Could you please provide more details on your requirement? If you could share screenshot and sample we might be able to help

Reply

SBX - Two Col Forum

SBX - Migrated JS