Introduction:
With CRM 2015 SP1, the Client API was extended to allow attaching events to sub-grid to manage the sub-grid data/operations.
The OnLoad event however executes on all of the following operations
- Load of parent form.
- Save of parent form.
- Add a new record in sub-grid
- Remove a record from sub-grid
- Navigating to prev/next page in sub-grid.
When we register a method on the OnLoad event, it does not indicate in any way which of the above actually fired the onload event.
Workaround to Identifying the operation
We cannot register an event when the record is added/removed from the sub-grid. The only event available is onload, so one way to identify if it was one of the add/remove operations that caused the grid to call the onload event, we can keep a check on the count of records in the grid.
We can register the onload event using the following code
//This function is executed onload event of form
function onLoad() { var funtionName = "onLoad"; try { //setting timeout beacuse subgid take some time to load after the form is loaded setTimeout(function () { //validating to check if the sub grid is present on the form if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null && Xrm.Page.getControl("contact_subgrid") != undefined) { //stores the row count of subgrid on load event of CRM Form _rowCount = Xrm.Page.getControl("contact_subgrid").getGrid().getTotalRecordCount(); //registering refreshform function onload event of subgrid Xrm.Page.getControl("contact_subgrid").addOnLoad(onGridLoad); } }, 5000); } catch (e) { Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description)); } }
The rowCount is defined as global variable and is updated here to store the count of records in the grid when the form is loaded initially.
//This function calls the intended function on load of subgrid
function onGridLoad() { var functionName = " onGridLoad "; var currentRowCount = null; try { //setting timeout beacuse subgrid take some time to load after the form is loaded setTimeout(function () { //validating to check if the sub grid is present on the form if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contact_subgrid") != null && Xrm.Page.getControl("contact_subgrid") != undefined) { //stores the row count of subgrid on load event of CRM Form currentRowCount = Xrm.Page.getControl("contact_subgrid").getGrid().getTotalRecordCount(); if (currentRowCount > _rowCount) { //call the intended function which we want to call only when records are added to the grid dosomething(); //set current row count to the global row count _rowCount = currentRowCount; } else if (currentRowCount < _rowCount) { //call the intended function which we want to call only when records are removed from the grid dosomethingelse(); //set current row count to the global row count _rowCount = currentRowCount; } } }, 2000); } catch (e) { Xrm.Utility.alertDialog(functionName + "Error: " + (e.message || e.description)); } }
Conclusion:
In this way we can identify if a record is added or removed from sub grid in Dynamics CRM 2015 SP1.
One Pic = 1000 words! Analyze data 90% faster with visualization apps!
Get optimum visualization of Dynamics 365 CRM data with –
Kanban Board – Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.
Map My Relationships – Map My Relationships – Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.
This post is very insightful.
Is it possible for an email to be sent when a record is added to a subgrid?
From the face of it we cannot retrieve the record that is added or subtracted from the sub grid.
We can only detect if a record is added or subtracted using the above code.
Thanks for this useful post!
what if I have more than one subgrid into on entity and I want to refresh the form after adding or removing any records on the entity subgrids, is it possible?
Yes, we can refresh the form after adding or removing any records on the entity sub grids.
For example, if we have two sub grids on the entity form then create two global variables, store the row count of sub grids in those variables on load event of CRM form and compare them with current row count of sub grids in the “onGridLoad” function which is registered on “addOnLoad” event of the sub grids as explained in the above blog.
Hope this helps.
Thanks,
Shall I add Xrm.Page.data.refresh(false); instead of dosomething(); in your code for getting the parent refresh?
Yes, You need to add the code or call the function you want to execute on adding or removing records from the sub grid instead of dosomething(); in above code.
Hope this helps.