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

Notifications

Announcements

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
iampranjal Profile Picture

iampranjal 67

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 37 Super User 2025 Season 2

#3
Pallavi Phade Profile Picture

Pallavi Phade 24

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans