Skip to main content

Notifications

Announcements

No record found.

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);
}
}
}

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

    I suspect setFilterXml isn't doing anything. When I debug, I am not seeing anything that tells me filter has been set to the grid. Otherwise, I see the results are coming through and the filter looks good with regardingobjectid etc. in debug mode.

    formContext.getControl("Subgrid_new_EventsMailMerge").setFilterXml(filter);

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

    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,executionContext);
            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("");
    
    formContext.getControl("Subgrid_new_EventsMailMerge").setFilterXml(filter);
    formContext.getControl("Subgrid_new_EventsMailMerge").refresh();
    }, Xrm.Navigation.openErrorDialog);
    }
    }
    }

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Custom filter for a Subgrid

    This part looks good. Can you please provide the current version of the code?

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

    Actually, my bad, I have posted wrong picture.

    pastedimage1677103174641v1.png

    This is the subgrid and there are no filters. Sorry for the confusion.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Custom filter for a Subgrid

    So... here is the answer. Change the "Records" dropdown to "All record types".

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

    Here is the grid configuration:

    pastedimage1677098669856v1.png

    query is basically status equals 'Active'. That's all.

    Thanks.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Custom filter for a Subgrid

    Hello,

    More or less. Can you please show your grid configuration (similar to the screenshot I provided earlier)? And can you please show the setting (basically query) of the view you use in the grid?

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

    Yes, it shows 'Activities' of a candidate's Registrations. This Candidate is a custom entity which has Email activities that goes out from it. There is no problem with displaying those activities because it is straightforward. However, each Candidate can have 'Registrations' for multiple Events. And emails go out from Registrations and these email activities that I am trying to show on Candidates' profile page as a Subgrid by modifying the filter. All I am trying to do is grab the Candidate's recordid and use it to get the 'Regardingobjectid' through which I am trying to retrieve Registration activities into the grid.

    So, I placed the 'Email' entity as the subgrid's datasource and placed a system view removing all the filters from it.

    Makes sense?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Custom filter for a Subgrid

    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

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Custom filter for a Subgrid

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

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Share Your Event with the Community

Jump start your event engagement! 📢

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,925 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,646 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans