CRM2011 – Set default Today’s date in the DataField

I had a task to customize a data field to set in automatic today’s date when open a form in New state.In this post I would discuss how we can do it. First of all we need to create the form and we put inside the data field:

Unbenannt

Now we can create a new web resource with the follow code:

function setTodayDate(date) {
    var isNewForm = 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
    }
}

Now on the form now we need to create a new Event Handler for the OnLoad event to call this function. We will also need to enter the date field we are PreLoad into the parameters area, which should look something like the image below:

1

And now we can preview our form, and see that the Date field of the our entity is being preload to today’s date when we create a new record. Because we set the date field with each Event Handler we add, we can reuse this function on any field of any entity to prefill a date field with today’s date.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...