Restrict changing a date field to further date from its current value Dynamics CRM?

Viewed 505

I want to achieve the following, restrict changing a date field to further date from its current value, in Dynamics CRM via JavaScript.

Below is my JavaScript code, im pretty new to JavaScript Dynamics development, below is my code, by the way its not throwing an alert, is it coded properly? - apologies if the question doesnt make sense.

Why isn't it throwing an alert, when I enter a date future from current value?

function test(executionContext) {
formContext = executionContext.getFormContext();
    
var fielddate = formContext.getAttribute('fielddate').getValue();
    
    if (fielddate != null) {
        
    fielddate.setHours(0, 0, 0, 0);
        
    //The current date
        
    var currentDate = new Date();
    
    currentDate.setHours(0, 0, 0, 0);
    
    if (fielddate > currentDate) {
        
    alert("You can't enter future date");

}
}
}
1 Answers

the JavaScript code is correct, you may want to check the field name (the one you are assigning to fielddate variable) because if it's null then your code is not executed.

Instead of alert I would write

Xrm.Navigation.openAlertDialog(text: "You can't enter future date");

because it's the supported way to display alerts (https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-navigation/openalertdialog)

after the message you should also clear the field, so the user can set a new value (otherwise the wrong value will be stay there)

formContext.getAttribute('fielddate').setValue(null);
Related