Dynamics CRM‎ > ‎Javascript Library‎ > ‎

Javascript to default a date/time field with Today's date


This code works if you need to set a date field in CRM to today's date when a new record is created.   Tested and built for CRM 2011.

Code:

// Prefill a Date Field with Todays Date 

function SetToday(date)
{    
var isCreateForm = Xrm.Page.ui.getFormType() == 1;
var dateField = Xrm.Page.getAttribute(date);
    if (isCreateForm)   // Check that this is a new Record
    {        dateField.setValue(new Date()); // Set the Date field to Today
             dateField.setSubmitMode("always");   // Save Disabled Fields
    }
}

To install:
 
1.  Create a Web Resource and add the above script to the Web Resource
2.  Add the Web Resource as a Form library and set the forms OnLoad event to the SetToday Function.
         a.   Customize the form, select FormProperties, Events Tab. 
         b.   Add a Library, Under EventHandlers, Control = Form, Event=OnLoad
         c.   In the function field, type in "SetToday"
         d.   In the box that says "comma separated list of parameters that will be passed to the function" add the Schema Name of the field you wish to default as a parameter.   Schema Name must be in double quotes (ie.  "myfield_schemaname")
 



Comments