web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Custom filter for a Subgrid

(0) ShareShare
ReportReport
Posted on by 7,316

Hi,

I am trying to setup a custom filter for one of my subgrids. It basically needs to show all the emails associated to a person(custom entity) sent from an entity(Activity enabled). Here is my script.. it does bring the filtered records as I checked while debugging, it just does not set the filter for some reason. Not sure how else to troubleshoot and so I need some help. Thanks for any help!

function SubGridFilterExecution(executionContext) {
    //Create a Form Context.
    var formContext = executionContext.getFormContext();
    //Step 1 - Get the subgrid control.
    var gridContext = formContext.getControl("Subgrid_new_EventsMailMerge");
    //Step 2 - Retrieving Form Attribute Value.
    var fullName  = formContext.getAttribute("new_personname").getValue();
	 //Step 3 - Recall the execution method if the subgrid context is null or empty.
	 
    if (gridContext == null) {
        setTimeout(SubGridFilterExecution, 3000);
        return;
    }
	else {
	 //Set grid with query A based fetch XML.
        if (fullName != null) {
            //Step 4 - Build a fetch XML in a variable.
            var FetchXmlA = ""  
                ""  
              ""  
                ""  
                ""  
				""  
				""  				
				""  				
				""  
                ""  
                ""  
                    ""  
                ""  
			    ""  
                "";

//Query the records
Xrm.WebApi.retrieveMultipleRecords("email", "?fetchXml="   encodeURIComponent(FetchXmlA)).then(
        function(results) {
            var functionalLocationsFilter = results.entities.map(
                function(r) { 
debugger;
                    return (""   r._regardingobjectid_value  "");
                }).join("");
//Composing the resulting filter for the grid
            var filter = [
                "",
                "",
                functionalLocationsFilter,
                "",
                ""
            ].join("");
			
		

gridContext.setFilterXml(filter);//debug stops here and exits. subgrid shows no data
gridContext.refresh();
}, Xrm.Navigation.openErrorDialog);
}
}
}

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    It looks like I know where you might take the code from - butenko.pro/.../ ;)

    Your code looks correct. One thing that I might think of is that if your first run doesn't return the grid (it's not loaded or something) then - setTimeout(SubGridFilterExecution, 3000); is called without the execution parameter. I would recommend trying to call it using the following code:

    setTimeout(SubGridFilterExecution, 3000, executionContext);

  • meenoo Profile Picture
    7,316 on at

    That is correct, Andrew. :)

    I will try setTimeout and get back. Thanks.

  • meenoo Profile Picture
    7,316 on at

    no luck.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I would recommend checking what your filter after manipulations looks like.

    Try following:

                var filter = [
                    "",
                    "",
                    functionalLocationsFilter,
                    "",
                    ""
                ].join("");
                
                console.log(filter);

  • meenoo Profile Picture
    7,316 on at

    filter looks good to me. Something weird happened though.. I did see a single record in the subgrid as expected but I refresh it brings all the records(that's the default view set to the subgrid). And sometimes it shows blank subgrid. Not sure what's going on!!

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    I'm not sure either. I used this approach multiple times and it always worked for me.

  • meenoo Profile Picture
    7,316 on at

    What should be the underlying view for the subgrid? Does it matter?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Yes, it matters. When you add the filter - it's applied to the view underneath the grid.

  • meenoo Profile Picture
    7,316 on at

    can the grid have any default filters in it? I tried removing all the filters and still won't work!

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    The grid that you're talking about - is it contextual? Is it filtering the data based on the record of the form? Check this part of my post:

    pastedimage1676564165792v1.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 83

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 76 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans