Form Event Programming in Dynamics 365
Microsoft Dynamics CRM forms are the primary interface used to interact with customer data. The more you customize your forms, the more you will imagine valuable ways to interact with your data.
Some of the most common examples of using form event programming are:
- Automatically default values of certain fields when the value of another field is set
- Trigger a lookup of some data based on the entered value of some field
- Create calculated values based on various field values on the form.
- Validate data entry by correcting invalid data
- Alert users to data issues
- Make fields visible/invisible or enabled/disabled based on various situations
The primary benefits of using form event programming to customize Microsoft Dynamics CRM behavior:
On Load Event
The OnLoad event occurs once when a CRM form has fully loaded. Each OnLoad event handler will execute depending on its order as specified in form properties. For example, if there are two event handlers registered for OnLoad event of a form, then script for event handler 1 will execute first then event handler 2. The OnLoad event can be used to perform data pre-population, form layout changes, or interactions with other page elements.
The OnSave event occurs once when the user clicks on any one of the three save buttons (Save, Save & Close and Save & New) or when code forces the form to save. Similar to OnLoad, any number of event handlers can be registered for OnSave of Form. OnSave event can be used for data validation and data manipulation.
The Save operation can be canceled if data validation fails.
The OnChange event occurs at the field level. OnChange event triggers whenever data in a form field changes or focus is lost. Data validation happens before and after the OnChange event. Similar to OnLoad, any number of event handlers can be registered for OnChange of a field.
All fields support the OnChange event. The OnChange event does not occur if the field is changed using script.
Tab TabStateChange Event
The TabStateChange event occurs when the DisplayState of the tab changes due to user interaction or forced by applied script. This event can be used for changing a URL for an IFrame at run time within a tab or can be used also for data manipulations.
The OnReadyStateComplete event triggers when content of the IFRAME has loaded fully. This event can be used while referencing IFRAME controls within scripts.