Microsoft Dynamics CRM exposes events for forms, fields, and other controls. These are the event handlers where we can call our custom JavaScript methods.
We can configure our client-side code to execute on specific events or dynamically associate our method to a corresponding event. At beginning we will understand events related to forms.
Form Events
There are two events available for MS CRM form –
1. OnLoad Event : The OnLoad event handler executes code when the entity form is loaded. We can utilize this event for controlling the behavior of entity forms and this event is useful for different scenarios. For example, we may want to hide/disable some fields based on other fields or based on the user security role. Business rules also utilize the OnLoad event to execute logic.
2. OnSave Event : The OnSave event is executed when the entity form is saved; for example it is executed when the user clicks on the Save button in the lower-right corner of the screen. It is also executed automatically after 30 seconds if auto-save is enabled.
It can also be executed using the following methods:
Xrm.Page.data.entity.save
Xrm.Page.data.save
Xrm.Page.data.refresh
We can also stop the save event, if required. Let's take an example. When all the data has been entered on the entity form and the user wants to save it to the CRM database, but before sending it to the server, let's say we want to validate the data by the user; if the validation fails, you can cancel the save event. We can use following code to cancel the save event:
Xrm.Page.context.getEventArgs().preventDefault();
Field Events
All entity fields have one event exposed: the OnChange event. The OnChange event fires when focus from the field is lost. So, as soon as we tab out from a CRM field by entering or selecting some value, our custom JavaScript code associated with the OnChange event will fire.
This statement is not true for set value fields if they are formatted as a radio button or checkbox. The OnChange event for these fields fires immediately instead of executing after the focus is lost.
Control Events
Apart from form and field events, there are specific control events that we can use for implementing client-side logic. Following are the other common events.
TabStateChange :
The TabStateChange event is associated with the display state of tab control, so it fires when the tab control display state changes. We can use this event to control fields and other controls such as loading the IFRAME control.
OnReadyStateComplete
This event is associated with the IFRAME control and occurs when the content of the IFRAME is loaded fully.
PreSearch
This event is associated with lookup controls. We can utilize this to filter lookup controls based on our specific requirements. We can't configure this event through the UI; instead, we use addPreSearch and removePreSearch to associate our JavaScript function with the PreSearch event.
No comments:
Post a Comment