In one of my previous blog I explained,how a rollup field can be used to count child records : Count the number of related child records using a Rollup field
However, Rollup field is calculated with an Async System Job, therefore, if you need something to be triggered on every form load; you will have to write a JS.
Here is a quick code to count the number of related sub-grid record on a form onload. You can put an alert of the count or add that number to a field.
function getTotalGridRecordCount() {
debugger;
try {
setTimeout(function () {
if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("contactopportunitiesgrid") != null && Xrm.Page.getControl("contactopportunitiesgrid") != undefined) {
var count = Xrm.Page.getControl("contactopportunitiesgrid").getGrid().getTotalRecordCount();
alert("Total Opportunities:"+count);
}
}, 5000);
}
catch (e) {
Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);
}
}
Add script to the form and call this function on Onload event. Open the form to see it in action:

Hope this helps! cheers!

Like
Report
*This post is locked for comments