CALL JAVASCRIPT FUNCTION ON SUBGRID REFRESH IN DYNAMICS 365
In this post, we will see an example on how to call JavaScript function when a subgrid is refreshed in Dynamics 365.
Sometimes, you have a scenario where you need to call a JavaScript function when a sub-grid refresh event has occurred in the form.
The subgrid refresh event might happen if a record is added to the subgrid, deleted from the subgrid, or the refresh button of the subgrid is clicked.
- In order to trigger the function, you have to register a function on the OnLoad event of the form (in the below exemple manageFormBasedOnRecordsInSubgrid) which in turns, will call the OnLoad event of the subgrid
- After doing this, each time you add/delete record from sub-grid, the function functionTriggerredgetOnSubgridRefresh() will be triggered.
-
Below, is a sample code for the function that will be called when the subgrid is refreshed
var formContext;
var gridContext;
function manageFormBasedOnRecordsInSubgrid(context) {
formContext = context.getFormContext();
gridContext = formContext.getControl("Contacts");
//ensure that the subgrid is ready…if not wait and call this function again
if (gridContext == null) {
setTimeout(function () { manageFieldsBasedOnLivrableSubgrid(); }, 500);
return;
}
//bind the event listener when the subgrid is ready
gridContext.addOnLoad(functionTriggeredgetOnSubgridRefresh);
}
function functionTriggeredgetOnSubgridRefresh() {
try {
setTimeout(function () {
if (gridContext != null && gridContext != undefined) {
var recordsCount = formContext.getControl("Contacts").getGrid().getTotalRecordCount();
if (recordsCount > 0) {
var alertMessage = { text: "Do something here when subgrid contains records" };
Xrm.Navigation.openAlertDialog(alertMessage, null);
}
else {
var alertMessage = { text: "Do something here when subgrid has no records" };
Xrm.Navigation.openAlertDialog(alertMessage, null);
}
}
}, 1000);
}
catch (e) {
var alertMessage = { text: "getTotalGridRecordCount Error: " + e.message || e.description };
Xrm.Navigation.openAlertDialog(alertMessage, null);
}
}
Hope This Helps!
Comments
Post a Comment