Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Filtering the Lookup Field In SubGrid

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi As I am new to CRM I dunno how to Filter the Lookup Field in Subgrid.
 I need to filter this field company name which I gave it as lookup. I need if the company name is A.datumfabrication . need to filter that record 
5861.pastedimage1580215677597v1.png
 here is my code 
function subgridlookup() {
try{
var entityName = Xrm.Page.data.entity.getEntityName();
if (entityName == "new_customer") {
var viewId =Xrm.Page.data.entity.getId();
var cmpny = Xrm.Page.getAttribute("new_customer").getValue();
if (cmpny != null) {
var sublookup= Xrm.Page.getControl("contacts").addPreSearch(preser());}}}
catch(e){
alert(e.message);}
function preser(){
var fetchxml=
"<fetch version='1.0' distinct='false' mapping='logical' output-format='xml-platform'>"+"<entity name='contact'>"+
"<attribute name='fullname'/>"+
"<attribute name='telephone1'/>"+
"<attribute name='contactid'/>"+
"<order descending='false' attribute='fullname'/>"+
"<filter type='and'>"+
"<condition attribute='parentcustomerid' value='{A56B3F4B-1BE7-E611-8101-E0071B6AF231}' uitype='account' uiname='A Datum Fabrication' operator='eq'/>"+
"</filter>"+
"</entity>"+
"</fetch>"
var layout = "<grid name='contacts' object='1' jump='name' select='1' icon='1' preview='1'>" +
"<row name='Company Name'>" +
"<cell name='fullname' width='150px' />" +
"<cell name='company name' width='150px' />" +
"</row>" +
"</grid>";
Xrm.Page.getControl("contacts").addCustomFilter("contacts",fetchxml,layout,viewId,true);

}
 Please asist me where i went wrong 

  • Suggested answer
    Aditya Profile Picture
    Aditya 260 on at
    RE: Filtering the Lookup Field In SubGrid

    Hi Jainy, 

    If you want to show the Contact records in the subgrid on the basis of Company  (Lookup of Account record i.e "A Datum Fabrication"  in your case)  on the Customer form, you can use the below code onload and onchange of Company field  :-

     function filterContactsGrid(executionContext) {

        try {

            //---Get the form context using execution context

            var formContext = executionContext.getFormContext();

     

            //---Suppose there is a subgrid named "contacts" placed on your entity's form

            var conSubGrid = formContext.getControl("contacts");

     

            //Get the lookup field on the basis of which you want to filter the subgrid

            var company = null;

            if (formContext.getAttribute("new_account").getValue() != null) {

                company = formContext.getAttribute("new_account").getValue()[0].id.replace("}", "").replace("{", "");

            }

     

            //Create FetchXML for sub grid to filter records

     

            //Set Grid to Empty if Company i.e Account is Null

            var fetchXml = ' <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">'

                              + ' <entity name="contact">'

                                + ' <attribute name="fullname" />'

                                + ' <attribute name="telephone1" />'

                                + ' <attribute name="contactid" />'

                                + ' <order attribute="fullname" descending="false" />'

                                + ' <filter type="and">'

                                  + ' <condition attribute="parentcustomerid" operator="null" />'

                                + ' </filter>'

                              + ' </entity>'

                            + ' </fetch>';

     

     

            if (company != null) {

                //Show Related Contacts associated with Customer with Company Name as ‘A Datum Fabrication’

                fetchXml = ' <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> '

                              + ' <entity name="contact"> '

                                + ' <attribute name="fullname" /> '

                                + ' <attribute name="telephone1" /> '

                                + ' <attribute name="contactid" /> '

                                + ' <order attribute="fullname" descending="false" /> '

                                + ' <filter type="and"> '

                                  + ' <condition attribute="parentcustomerid" operator="eq" uiname="A Datum Fabrication" uitype="account" value="{A56B3F4B-1BE7-E611-8101-E0071B6AF231}" /> ' // you can use company here for dynamic filtering

                                + ' </filter> '

                              + ' </entity> '

                            + ' </fetch> ';

             }

           

            formContext.getControl("contacts").setFilterXml(fetchXml);

     

            //Refresh the grid to show filtered records only.

            formContext.ui.controls.get("contacts").refresh();

        }

         catch(e){

            alert(e.message);}

    }

     Hope it helps!!

    Please mark the answer as verified if it is useful to you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Filtering the Lookup Field In SubGrid

    yes

  • Aditya Profile Picture
    Aditya 260 on at
    RE: Filtering the Lookup Field In SubGrid

    Do you need to show only those Contact records in the subgrid which has Account as 'A Datum Fabrication' on the Customer form? Please Confirm?

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Filtering the Lookup Field In SubGrid

    Hello ,

    I think you can do this without writing any code using quick view form.  Create a quick view form in the Account Entity  where add the sub grid of contact entity.

    Once you created quick view form you just simply include quick view form in attach screenshot form referencing the lookup.

    https://www.magnetismsolutions.com/blog/adammurchison/2017/09/14/how-to-add-a-quick-view-form-in-microsoft-dynamics-365

  • Abigail Profile Picture
    Abigail 550 on at
    RE: Filtering the Lookup Field In SubGrid

    Here it is:

    A common requirement is to apply filtering to a subgrid’s inline lookup on CRM form. For example, you might want to allow users to add only contacts of a particular account (selected in another field) to a contact subgrid on the form.

    Just to be clear, the highlighted lookup below is what we want to filter (and what this post is about). There are several posts on the web that talk about filtering the subgrid, but they are about filtering the records that appear on a subgrid, and not the records that appear in the inline lookup that is used to add a record to the subgrid.

    1-lookup

    Note: The method described in this post is unsupported.

    The inline lookup of a subgrid is actually a normal lookup control (more or less) and therefore has the same methods as a normal lookup control. We, therefore, can apply filtering to this control using the standard addPreSearch and add custom filter methods of the control.

    It is, however, difficult to gain access to this control (for a subgrid) as it is not exposed in any of the API. We, therefore, will need to use unsupported methods to access it.

    The subgrid’s lookup control is actually given a control name by CRM, and this name is based on the name of the subgrid. For example, if the subgrid is named grdContacts in the form, the name of the subgrid’s lookup control would be lookup_grdContacts. We can use this name to retrieve the lookup control – but wait.

    While you can use Xrm.Page.getControl(“lookup_grdContacts”) to retrieve the subgrid’s lookup, you can only do so after the user has clicked the + icon on the subgrid to bring up the lookup control. Therefore, our code to retrieve the lookup control and call the addPreSearch and addCustomFilter methods on it would need to happen within (or triggered by) the click event of the + icon.

    The HTML ID of the + icon has the format [gridName]_addImageButton, e.g. grdContacts_addImageButton. Using this ID we can retrieve the + icon from the DOM and attach code to the click event to retrieve the lookup control and apply our filter.

    Below is the code. Please review the in-code comments.

    function setSubgridLookupFiltering() {

       var subgridAddButtonId = "grdContacts_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_grdContacts");

           //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) {

               return;

           }

           subgridLookup.addPreSearch(function() {

               //Standard logic to build up the filter query string

               var filterQuery = "";

               //filterQuery = "<filter type='and'.....></filter>";

               //..

               //..

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

       });

    }

    I hope this helps!

    Abigail | MS Dynamics 365 user.

  • erhan.keskin Profile Picture
    erhan.keskin 2,251 on at
    RE: Filtering the Lookup Field In SubGrid

    Hi,

    My understanding is that you want to show only related records of the main record in sub grid. In order to do that,

    • open the properties of the subgrid
    • Select "Only Related Records" in the "Records" field of the "Data Source" section

    If that is not what you want then check this out to see more about how to filter sub grid; https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/221757/filter-subgrid-records

    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

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! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans