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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

fetchXml with Linked Entities on a Subgrid unable to find link-entity

(0) ShareShare
ReportReport
Posted on by 229

Hello community,

I'm having issues with implementing an advanced filter on a Subgrid (entity 'email', shown on a 'contact' form as the subgrid 'emailGrid').

What I'd like to do is show all e-mails that contain the emailaddress1 of the current contact record, even if it's related to the systemuser of the same person. 

To do so I've built a fetchXml query in the FetchXML Builder:

  
    
    
    
    
    
    
      
      
    
    
    
      
        
      
      
        
      
    
  

However, I'm facing two issues with that query that I hope someone will be able to help me with:

a) When I add this to the subgrid, the grid itself tells me that "Link entity with name or alias contact is not found". I've tried using different alias (eg "ac" for the contact, "asu" for the systemuser) to no avail. The query works perfectly fine (aside from not being able to enable 'distinct' and therefore showing duplicate records) from within FetchXML Builder.

function updateSubGrid(executionContext) {
    var formContext =  executionContext.getFormContext();
    //var mailGrid = document.getElementById("emailGrid");
    //var mailGrid = window.parent.document.getElementById("emailGrid");
    var mailGrid = formContext.getControl("emailGrid");
    if (mailGrid == null) {
        console.log("mailGrid does not exist, retry in 1s.");
        setTimeout(function () { updateSubGrid(executionContext); },1000); // wait a second for the grid to load, then retry
        return;
    }
    
    var emailAddress = formContext.getAttribute("emailaddress1").getValue();
    console.log("set filter to emails from/to this email: " emailAddress);
    if (emailAddress == null) return;
    var fetchXml = "" 
    "" 
        "" 
        // Columns to retrieve
            "" 
            "" 
            "" 
            "" 
            "" 
        // Filter based on emailAddress through both linked entities
            "" 
                "" 
                "" 
            "" 
            // Order by sent date
            "" 
            // Link to activityparty
            "" 
                // and link to Contact and Systemuser 
                "" 
                    "" 
                "" 
                "" 
                    "" 
                "" 
            "" 
        "" 
    "";
    console.log(fetchXml);
    mailGrid._gridControl.setFilterXml(fetchXml);
    console.log("filter was set, refreshing grid.");
    mailGrid._gridControl.refresh();
}

b) When I try adding distinct='true' to the query, FetchXML Builder tells me "An unexpected error occured." without telling me what kind of error, then offers to try as ExecuteFetch, which DOES work.

I have the same question (0)
  • DKasp Profile Picture
    229 on at
    RE: fetchXml with Linked Entities on a Subgrid unable to find link-entity

    Hey David,

    That solved my issue after a bit of tinkering, thank you very much!

    What I've done to get it to work:

    * Create a view for the E-Mail entity

    * Load that view in FetchXML Builder

    * Modify it to have everything from my FetchXML, excluding the filter/conditions

    * Save the changes directly to the View in CRM

    Now all I had to do is have the Javascript "inject" the conditions by using setFetchXml().

  • Verified answer
    David Jennaway Profile Picture
    14,065 on at
    RE: fetchXml with Linked Entities on a Subgrid unable to find link-entity

    For your first issue, the problem is likely that the aliases in the fetchxml don't match the layoutxml. Any view used in CRM (e.g. in a subgrid) has both a fetchxml (to define the query) and a layoutxml (to specify how to display the results), and the layoutxml will reference columns based on the fetchxml, so if you modify just the fetchxml, you can get the error.

    You can use FetchXML builder to query the savedquery entity to view the layoutxml

    As to the distinct="true", you may find that the issue goes away. By default, CRM will not show duplicate records in a grid, so I think that, once the layoutxml is fixed, then you won't need distinct="true". (My suspicion is that CRM is adding this automatically, so the attribute is duplicated, which causes the error)

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

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans