Dynamically Hide the “New” Button in a Dynamics 365 Sub-Grid

In some business scenarios users should only add one (or two) child records. The quickest way to enforce this in Dynamics 365 is to hide the New button after the sub-grid reaches a specific row count. This article shows how to:

  1. Create a JavaScript enable rule that counts rows.
  2. Refresh only that sub-grid’s ribbon so the rule re-evaluates instantly—no full form reload.

1  Enable Rule – Show Button Only When < 2 Rows Exist

/**
 * sgEnableNewButton
 * -----------------
 * Enable rule: show the “New” button only when record count < 2.
 *
 * @param {Xrm.Events.SelectedControl} control – sub-grid control context.
 * @returns {boolean} true ⇒ button visible, false ⇒ hidden
 */
function sgEnableNewButton(control)
{
    var cnt = control?.getGrid()?.getTotalRecordCount() ?? 0;
    return cnt < 2;
}

Ribbon Workbench / Command Designer

  • Add Enable Rule → Custom JavaScript
  • Function Name: sgEnableNewButton
  • Pass Parameter: SelectedControl

2  Refresh the Grid Ribbon Every Time the Grid Reloads

Add the script below as a web resource and register registerOnLoadSubgridAppeal on the main form’s On Load event (remember to tick Pass execution context).

/**
 * registerOnLoadSubgridAppeal
 * ---------------------------
 * Wire one OnLoad handler to the “Appeal” sub-grid that refreshes its ribbon
 * whenever the grid reloads.  Call this in the main form’s OnLoad.
 *
 * June 24 2025 – Forrest • Task D4P-333
 */
function registerOnLoadSubgridAppeal(executionContext)
{
    var formContext = executionContext?.getFormContext();
    if (!formContext) { return; }

    var sg = formContext.getControl("subgrid_appeal");  // ← grid name
    if (!sg?.addOnLoad || !sg?.removeOnLoad) { return; }

    sg.removeOnLoad(appealSubgridOnLoad);   // ensure single registration
    sg.addOnLoad(appealSubgridOnLoad);
}

/**
 * appealSubgridOnLoad
 * -------------------
 * Sub-grid OnLoad callback – refreshes this grid’s ribbon only.
 *
 * June 24 2025 – Forrest • Task D4P-333
 */
function appealSubgridOnLoad(executionContext)
{
    var formContext = executionContext?.getFormContext();
    var gridCtl     = formContext?.getControl("subgrid_appeal");

    gridCtl?.refreshRibbon?.();   // modern control API
}

3  Publish & Test

  1. Save and publish the JavaScript web resource.
  2. Publish the ribbon changes.
  3. Open a parent record and add child rows:
    • After the second row is saved, the sub-grid flashes and the New button disappears.
    • If you delete a row, the ribbon refreshes again and the button re-appears.

Why Ribbon-Only Refresh Beats a Full Form Refresh

Full Form Refresh Sub-Grid Ribbon Refresh
Reloads every control; fires form-level scripts and events. Touches only one grid; no extra events.
Can trigger autosave prompts and slow the user. Instant UX—no autosave warning.
May cause side-effects in other custom scripts/plugins. Isolated and predictable.

With this lightweight pattern you keep the interface snappy while enforcing your business rule on child record counts—no more unnecessary form refreshes!

No comments:

Post a Comment