Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

fetchxml linked-entity with lookup filter

(0) ShareShare
ReportReport
Posted on by

hello,

in the entity serviceappointment  i add a lookup  field "new_contract'  to diplay the list of contract, 

iwant to display the list of contract of the selected account,

so i create a fetchxml filter and i added it to javascript function but that doesn't work ;

function preFilterLookup() 
{   
    Xrm.Page.getControl("new_contrat").addPreSearch(function () 
    {
        addLookupFilter();

    });
}

function addLookupFilter() 
{
    var customer= Xrm.Page.getAttribute("customers").getValue();
    if (customer!= null) 
    {
        fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"
       + "<entity name='serviceappointment'>"
         +"<attribute name='customers' />"
         +"<filter type='and'>"
         +"<condition attribute='regardingobjectid' operator='eq' value='" + customer + "'  />"
         +"</filter>"
         +"<link-entity name='contract' from='contractid' to= 'new_contrat'>"
           +"<filter type='and'>"
            +" <condition attribute='customerid' operator='eq' value='" + customer + "' />"
           +"</filter>"
        +" </link-entity>"
       +"</entity>"
     +"</fetch>";
  
  
        Xrm.Page.getControl("new_contrat").addCustomFilter(fetchXml);    
    }
}


 

*This post is locked for comments

  • Verified answer
    Aileen Gusni Profile Picture
    Aileen Gusni 44,524 on at
    RE: fetchxml linked-entity with lookup filter

    Hi Yosra,

    Thank you for your confirmation.

    And the 'customer' value is using Guido's suggestion:

    =================================

    if (customer!= null) {

    customerid = customer[0].id;

    and put customerid variable inside the fetchxml

    =================================

    Because it is referring to a special activity party.

    If our answers helped you, you can help the others by closing this thread and verifying the answers.

    Thanks.

  • yosra.walid Profile Picture
    yosra.walid on at
    RE: fetchxml linked-entity with lookup filter

    Yes that work for me with this filter

    fetchXml = "<filter type='and'>"

              + " <condition attribute='customerid' operator='eq' value='" + customer + "' />"

             + "</filter>";

  • Suggested answer
    Aileen Gusni Profile Picture
    Aileen Gusni 44,524 on at
    RE: fetchxml linked-entity with lookup filter

    Hi Yosra,

    So, you can use addCustomFilter with your fetchxml to filter Contract by Customer (linked entity)

    use this filter fetch xml

    fetchXml = "<filter type='and'>"

               + " <condition attribute='customerid' operator='eq' value='" + customer + "' />"

              + "</filter>";

    Can you help to confirm it so that the others can use the same method.

    Thanks.

  • Verified answer
    yosra.walid Profile Picture
    yosra.walid on at
    RE: fetchxml linked-entity with lookup filter

    hello, thanks for your answers

    that help me

    i  taked the id of the customer, 

    and in the filter i remove the linked entity and the fetch, i just leave the filter node xml

    so the correct code is 

    function preFilterLookup() {
        Xrm.Page.getControl("new_contrat").addPreSearch(function () {
            addLookupFilter();
    
        });
    }
    function getlookupCustomer()
    {
        var lookupObject = Xrm.Page.getAttribute("customers");
    
        if (lookupObject != null) {
    
            var lookUpObjectValue = lookupObject.getValue();
    
            if ((lookUpObjectValue != null)) {
    
                var lookuptextvalue = lookUpObjectValue[0].id;
                return lookuptextvalue;
    
            }
        }
    }
    function addLookupFilter() {
        var customer = getlookupCustomer();
        if (customer != null) {
            fetchXml = "<filter type='and'>"
                + " <condition attribute='customerid' operator='eq' value='" + customer + "' />"
               + "</filter>";
            Xrm.Page.getControl("new_contrat").addCustomFilter(fetchXml);
        }
    }
    

  • Suggested answer
    Aileen Gusni Profile Picture
    Aileen Gusni 44,524 on at
    RE: fetchxml linked-entity with lookup filter

    Yosra,

    Yout fetch xml here:

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

          + "<entity name='serviceappointment'>"

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

            +"<filter type='and'>"

            +"<condition attribute='regardingobjectid' operator='eq' value='" + customer + "'  />"

    +"</filter>"

            +"<link-entity name='contract' from='contractid' to= 'new_contrat'>"

              +"<filter type='and'>"

               +" <condition attribute='customerid' operator='eq' value='" + customer + "' />"

              +"</filter>"

        +" </link-entity>"

          +"</entity>"

        +"</fetch>";

    is not supported as xml for addCustomFilter() method.

    It only supports <filter> node XML:

    "<filter type='and'>"

            +"<condition attribute='regardingobjectid' operator='eq' value='" + customer + "'  />"

    +"</filter>"

    It does not support the linked entity: +"<link-entity> to put in the XML for addCustomFilter() method.

    But as I know it didn't event prompt you an error, but the data is still not filtered as per expectation without any pop up message.

    You can use a addCustomView() if you want to use your current fetch XML.

    msdn.microsoft.com/.../gg334266.aspx

    But, you need to construct your own layoutXML and you might have problem found that the grid is appearing inconsistent.

    See this post:

    community.dynamics.com/.../134494.aspx

    Or you can use this filter if you use addCustomFilter() method, for example:

    <filter type="and">

         <condition attribute="customerid" operator="in">

           <value>{1B0CF2DC-BE2D-E411-9451-001CC4EECDD4}</value>

         </condition>

       </filter>

    But, it will only filter one condition, cannot to have multiple condition of linked entity (you have condition regarding to, contract, etc)

    missdynamicscrm.blogspot.com/.../crm-2013-using-addcustomfilter-to-get-filtered-lookup-field-based-on-linked-entity.html

    Which the ID 1B0CF2DC-BE2D-E411-9451-001CC4EECDD4}) is you get from what Guido suggested before:

    And then, if you want to filter a Contract List, then you should not use:

    "<entity name='serviceappointment'>"

    entity name as serviceappointment, instead, you should use Contract.

    Yes, you need to go Advanced Find, do the query and then you can download the XML.

    Use the XML to contrsuct the addCustomView()

    msdn.microsoft.com/.../gg334266.aspx

    Example:

    www.powerobjects.com/.../creating-custom-filtered-lookup-crm-2011

    Hope this helps!

    Thanks.

  • Royal King Profile Picture
    Royal King 27,686 on at
    RE: fetchxml linked-entity with lookup filter

    addPreSearch does not work with linked entity conditions.

    Check the below response from Joe

    community.dynamics.com/.../119649.aspx

    You aren't going to be able to use addCustomFilter() but you will be able to use addCustomView.

    The cleanest, and most importantly, the *supported* way to handle your requirement is as follows:

    1. Download the FetchXML for the default query for the lookup (use advanced find).

    2. Download the FetchXML for the query that includes your custom filter (i.e. add the link-entity filter in advanced find).

    3. Create a javascript function that takes the two FetchXML queries as strings and conditionally replaces the default view using Xrm.Page.getControl().addCustomView()

    Using this method you can go multiple layers deep, conditionally cross filter among related lookups, etc. Pretty powerful stuff. addCustomFilter() is pretty compact but also very limited. addCustomView() removes the restrictor plate.

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

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans