When working with Microsoft Dynamics CRM, there are situations that require validation of data on the forms.  One of the more recent requests I have come across is validating data in certain fields based off the current date.  This can be useful when working with Opportunities to make sure salespeople are not putting in the wrong dates in certain fields or even on Contract’s to make sure the start and end dates make sense.

Below is a piece of jscript that gets the current date and sets that value into a field so it can be used to do some validation on the form.


function setToday(date)


var contractstatus = Xrm.Page.getAttribute(‘new_contractstatusapproved’).getValue()


if (contractstatus == 0)




var isCreateForm = Xrm.Page.ui.getFormType() != 4;

var dateField = Xrm.Page.getAttribute(date);

if (isCreateForm)


// Check that this is not a disabled record

dateField.setValue(new Date());

dateField.setSubmitMode(“always”);  }


Let’s break this down.

The variable “contractstatus” is getting the value of the contract status field so we can use it later on.

The variable “isCreateForm” is checking what state the form is in i.e. Create, Update, etc.

Next “dateField” is going to capture attribute that we want to set the current date for.

Once you have entered the desired values into these variables you have to call the function on the form.  It is up to you if you just want to call it OnLoad or OnChange of certain field, this really depends on the requirements of the business.  Now that the function is added, make sure you add the field name into the paramters area right below where the function is and check the pass execution context box.

At this point the code should be setting the current date into your desired field and you can write other pieces on script to do vaildation based off that date or you could even build some validation into the code above.

If you are looking to configure your version of Microsoft Dynamics CRM,  you can reach our professionals via email at [email protected] or by phone at 855.437.7202.

By: John Annunziato, RSM – www.mcgladrey.com/dynamics

Tagged with →