From recent working with Dynamics 365 customers transitioning to the new Unified Interface – have a CRM Sales User scenario that needs to set a CRM Contact field to the current date on a new record. The user can then either keep the default current date or set it to another value before saving the new record.
We will need to use JavaScript to set a CRM date field via the Unified Interface or the CRM legacy web client. The Dynamics 365 Business Rule does not offer a “Current Date” function at this time. Also Dynamics 365 Workflow or Microsoft Power Automate will not be suitable since it needs the CRM record to be saved in-order to trigger setting a default value (even CRM Workflow running in Real-Time would not work in this case).
So here is the JavaScript – working for both the Unified Interface and CRM legacy web client:
//————–
var Sdk = window.Sdk || {};
(function () {
this.setFieldCurrentDate = function (executionContext) {
var formContext = executionContext.getFormContext();
if(formContext.ui.getFormType()==1) { // 1 is for new record create state
var currentDate = new Date();
formContext.getAttribute(“birthdate”).setValue(currentDate);
}
}
}).call(Sdk);
//————–
To setup the above JavaScript:
Add the above JavaScript to a Web Resource
Add the above Web Resource to the CRM Contact Form Main Library and add “Sdk.setFieldCurrentDate” to the Form Onload event. Be sure to check on the “Pass execution context as first parameter”. Publish the CRM Contact entity.
On a New CRM Contact Record – the Contact’s Birthday field is defaulted to today’s date:
Contact me if you need some Microsoft Dynamics 365 CRM Sales Customer Engagement assistance.
Frank Lee
12 times awarded Microsoft MVP – Dynamics 365 / CRM
San Francisco Bay Area | Silicon Valley