You can do something like this.
Create a new field that tracks the number of records added to the sub grid.
For example: no_of_records
Create and add below function onload of the form with function name "Sdk.formOnLoad"
Run the workflow or power automate when "no_of_record" field is updated.
var Sdk = window.Sdk || {};
(function () {
this.formOnLoad = function ()
{
debugger;
var gridContext = Xrm.Page.getControl("your_subgrid_name_here"); // sub grid name
gridContext.addOnLoad(Sdk.gridOnLoad);
}
this.gridOnLoad = function()
{
debugger;
var gridContext = Xrm.Page.getControl("your_subgrid_name_here");
var grid = gridContext.getGrid();
var totalNumberOfRecord = grid.getTotalRecordCount();//no of record
var rows = grid.getRows();
if(totalNumberOfRecord == 0)
{
setTimeout(Sdk.gridOnLoad, 3000);
}
if ( totalNumberOfRecord != null){
Xrm.Page.getAttribute("no_of_records").setValue(totalNumberOfRecord);
}else{
Xrm.Page.getAttribute("no_of_records").setValue(0);
}
}
}).call(Sdk);