Search code examples
powerappspowerapps-modeldriven

Is there any way to Filter a Sub Grid Dynamically in Model Driven App


I am having Sub-Grid in a Form where, I need to filter a Column as per the data from other feild of a table. I have been trying to achieve this using Java Script but not sure getting the error an "undefined". Tried out many way 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 not able to Set the filter Criteria. Any help will be appreciated, thanks in advance.


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/