Implementing Dynamic Subgrid Filtering in Power Apps Model-Driven Forms Introduction

 In the Rental Smart MVP project, we often need to display filtered data in subgrids on forms like the Property form. This helps property managers quickly view owner expenses without clutter. Here's a simple JavaScript function to dynamically filter the Owner Expense History subgrid for Bills that are historical (Paid or Void), linked to the property owner, and not unit-specific. It uses setFilterXml for UCI compatibility and assumes the subgrid is set up as a related entity grid for Charges.

The Code

javascript
/**
 * setOwnerExpenseHistorySubgridFilter
 * ----------------
 * Applies dynamic filter XML to the Owner Expense History subgrid on the Property form, 
 * adding conditions for Bills (owner expenses) that are historical, for the Property owner, 
 * and not unit-specific. Assumes subgrid is configured as related Charges. *
 * November 08, 2025: Created by Forrest for dynamic subgrid filtering.
 */
function setOwnerExpenseHistorySubgridFilter(executionContext)
{
    var formContext = executionContext.getFormContext();
    
    var historySubgrid = formContext.getControl("Subgrid_new_ownerExpenseHistory");
    if (historySubgrid == null)
    {
        return;
    }
    
    var ownerAttr = formContext.getAttribute("hx_propertyownerid");
    if (ownerAttr == null)
    {
        return;
    }
    var ownerValue = ownerAttr.getValue();
    if (ownerValue == null || ownerValue.length === 0)
    {
        return;
    }
    var ownerId = ownerValue[0].id.replace("{", "").replace("}", "");
    var ownerType = ownerValue[0].entityType;
    
    var historyFilterXml = '<filter type="and">' +
        '<condition attribute="hx_documenttype" operator="eq" value="123200001" />' +
        '<condition attribute="hx_partyid" operator="eq" uiname="" uitype="' + ownerType + '" value="' + ownerId + '" />' +
        '<condition attribute="statuscode" operator="in">' +
        '<value>2</value>' + // Paid
        '<value>123200002</value>' + // Void
        '</condition>' +
        '<condition attribute="hx_unitid" operator="null" />' +
        '</filter>';
    
    historySubgrid.setFilterXml(historyFilterXml);
    historySubgrid.refresh();
}

How It Works

  1. Null Checks: Checks for subgrid control, attribute, and value to avoid errors.
  2. Dynamic Filter: Builds XML to add conditions (Bill type, owner party, historical statuses, null unit).
  3. Apply and Refresh: Sets the filter on the subgrid and refreshes it directly in form OnLoad—no addOnLoad to prevent loops.
  4. Assumptions: Subgrid linked via hx_propertyid; call this in form OnLoad event.

Usage in Rental Smart

  • Add to Property form's JavaScript library.
  • Update subgrid name if different.
  • Test in Dev env: Create Bills with hx_propertyid set, hx_unitid null, and vary statuses.

This keeps the UI clean and compliant with BC RTB tracking. If issues, check UCI mode or base query.

No comments:

Post a Comment