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 CRM (Archived)

Filter New Record SubGrid with field

(0) ShareShare
ReportReport
Posted on by

Hello,

I need filter a sub-grid search when i add a new record.

Scenario:
In my entity have a relationship 1:N with contacts, this relationship i put a subgrid in a form, i need filter the search contact by field (step 1), when i add a new record need tho show only contacts associated a account selected en the field of step 1, it's possible this function?

PD: Sorry for my english

*This post is locked for comments

I have the same question (0)
  • Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    This post should help you to implement your requirement: missdynamicscrm.blogspot.com.au/.../filter-lookup-field-by-related-subgrid-crm-2011-2013.html

    Thanks

  • Verified answer
    Community Member Profile Picture
    on at

    Hi, the information in this post doesn't works for me.

    But I found a solution for my case with relations N:N

    var LastQuery = "";
    
    function filterSubGrid() {
        debugger;
        setSubgridLookupFiltering();
    }
    
    function AddLookupFilter(entity_type, customFilter) {
        var subgridLookup = Xrm.Page.getControl("lookup_Contacts_Participants");
        subgridLookup.addCustomFilter(customFilter, entity_type);
    }
    
    function setSubgridLookupFiltering() {
        var subgridAddButtonId = "Contacts_Participants_addImageButton";
     
        //Try to get the element from both the current and the parent document.
        var subgridAddButton = document.getElementById(subgridAddButtonId) || window.parent.document.getElementById(subgridAddButtonId);
     
        //This script may run before the subgrid has been fully loaded on the form. If this is the case,
        //delay and retry until the subgrid has been loaded.
        if (subgridAddButton == null) {
            setTimeout(setSubgridLookupFiltering, 2000);
            return;
        }
     
        //Local function to retrieve the lookup control and apply the filter. We will queue this function in the click event handler of the
        //Add button's click event.
        var getSubgridLookupAndAddFilter = function() {
            var subgridLookup = Xrm.Page.getControl("lookup_Contacts_Participants");
     
            //Delay and retry until we can locate the lookup.
            if (subgridLookup == null) {
                setTimeout(getSubgridLookupAndAddFilter, 200);
                return;
            }
     
            //This is a custom property we are tagging on to the lookup control to ensure that we will
            //apply the custom filter only once when the Add button is first clicked.
            if (subgridLookup.customFilterAdded) {
                subgridLookup.removePreSearch(function () {
                    subgridLookup.addCustomFilter(LastQuery, "contact");
                    //AddLookupFilter("contact", LastQuery);
                });
            }
    
            var LookUp = Xrm.Page.getAttribute("jar_cliente").getValue();
    
            console.log(LastQuery);
     
            subgridLookup.addPreSearch(function() {
                //Standard logic to build up the filter query string
                var filterQuery = "<filter type='and'><condition attribute='parentcustomerid' operator='eq' value='" + LookUp[0].id + "' /></filter>";
                
    //Set Global Variable to remove PreSearch
    LastQuery = filterQuery; //Standard call to add filter to the lookup control subgridLookup.addCustomFilter(filterQuery, "contact"); }); //Mark the fact that we have already added the filter so that we won't do it again the next time the user clicks the Add button. subgridLookup.customFilterAdded = true; }; //Attach the function to retrieve the lookup and apply the filter to the Add button's click event. Remember that we //can only get the lookup after the user has clicked the Add button. subgridAddButton.addEventListener("click", function() { setTimeout(getSubgridLookupAndAddFilter, 200); }); }

    Only a one problem, when the user change the principal lookup field, not work the filter, the function is called removePreSearch.

    if (subgridLookup.customFilterAdded) {
                subgridLookup.removePreSearch(function () {
                    subgridLookup.addCustomFilter(LastQuery, "contact");
                    //AddLookupFilter("contact", LastQuery);
                });
            }
    


    I'm working for solved this problem.

  • jcadle Profile Picture
    5 on at

    I have been all over the internet for 2 days and this has been the only implementation of this code set that I have been able to get functional for an on prem 2016 version of Dynamics CRm. Thank you for posting.

  • Community Member Profile Picture
    on at

    I'm glad to have helped, best regards

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans