powerappspowerapps-modeldriven

Is there any way to filter a sub-grid dynamically in a Model Driven App


I have a sub-grid in a form where I need to filter a column as per the data from another field of a table. I have been trying to achieve this using JavaScript, but I'm getting an error "undefined". I tried out many ways, but nothing has helped.

var conSubGrid = Xrm.Page.getControl("Subgrid_new_9");
conSubGrid.setFilterXml("<filter><condition attribute=’cr85c_Nameid’ operator=’eq’ 
    value=’ABCD’ /></filter>");
    conSubGrid.refresh();

I am able to fetch the current grid details, but I'm not able to set the filter criteria.


Solution

  • Please note that setFilterXml is an undocumented javascript api for subgrid so there might be possible deprecation or removal of this api in future.

    As per the samples avaialable in other forums you have to pass the whole fetchXML in the setFilterXml and not just the filterxml.

    Update: modified below function to get fetchXml and modify to append the filterxml and setFetchXml & Refresh

    function SubGridFilterExecution(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var gridContext = formContext.getControl("Subgrid_new_9");
    
        var fetchXml = gridContext.getFetchXml();
    
        fetchXml = fetchXml.replace("</entity>", "<filter><condition attribute='cr85c_Nameid' operator='eq' value='ABCD' /></filter></entity>");
    
        gridContext.setFilterXml(fetchXml);
    
        gridContext.refresh();
    }
    

    https://www.crmcrate.com/javascript/dynamics-365-crm-filter-the-sub-grid-dynamically-with-javascript/