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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / N:N Lookup Record Filter
Power Apps
Answered

N:N Lookup Record Filter

(1) ShareShare
ReportReport
Posted on by 78

Hi Community,

I have a scenario involving N:N relationships and need some advice:

  • I have an ABC entity that has a 1:N relationship with Contact.
  • The ABC entity also has an N:N relationship with the XYZ entity.
  • Additionally, the XYZ entity has a 1:N relationship with Contact.

Now, when I use the “Add Existing” option to associate XYZ records with ABC, I want the lookup to only display those records that are linked to the same Contact associated with the ABC record.

Is there a recommended way to filter the lookup this way? Any references or advice would be greatly appreciated!

I have the same question (0)
  • Verified answer
    Jon Unzueta Profile Picture
    1,827 Super User 2025 Season 2 on at
     

    hanks for the detailed scenario—this is a common challenge when working with N:N relationships in model-driven Power Apps or Dynamics 365.

    To summarize your setup:

    • ABC (custom entity) has:
      • A 1:N relationship with Contact.
      • An N:N relationship with XYZ (custom entity).
    • XYZ also has a 1:N relationship with Contact.
    • You want the “Add Existing XYZ” lookup on ABC to only show XYZ records linked to the same Contact as the one associated with the ABC record.

    ✅ Recommended Approach

    To filter the lookup in the “Add Existing” dialog, you’ll need to use a custom JavaScript web resource with a custom filter on the lookup field. Unfortunately, out-of-the-box filtering for N:N lookups in the “Add Existing” subgrid is limited, but here’s how you can approach it:


    🔧 Option 1: Use a Custom Lookup + JavaScript Filtering

    1. Replace the N:N subgrid with a custom lookup field (or a custom subgrid with a button to launch a filtered lookup).
    2. Use JavaScript to filter the lookup based on the Contact associated with the current ABC record.

    Here’s a simplified example of how the JavaScript might look:

    function filterXYZLookup(executionContext) {
        var formContext = executionContext.getFormContext();
        var contactId = formContext.getAttribute("your_contact_field").getValue();
     
        if (contactId) {
            var lookupControl = formContext.getControl("your_xyz_lookup_field");
            lookupControl.addPreSearch(function () {
                lookupControl.addCustomFilter(
                    `<filter>
                        <condition attribute='contactid' operator='eq' value='${contactId[0].id}' />
                    </filter>`,
                    "xyz"
                );
            });
        }
    }

    You’d register this function on the OnLoad event of the form and possibly on the OnChange of the Contact field.


    🔧 Option 2: Use a Custom Dialog or PCF Control

    If you want to keep the N:N relationship but need more control over the selection process:

    • Create a custom command button on the subgrid using the Ribbon Workbench.
    • Launch a custom dialog (e.g., using a Canvas App or a custom page) that:
      • Filters XYZ records based on the Contact.
      • Allows selection.
      • Creates the N:N association programmatically using Web API.
    🏷️ Tag me if you have any further questions or if the issue persists.
    ✅ Click "Accept as Solution" if my post helped resolve your issue—it helps others facing similar problems.
    ❤️ Give it a Like if you found the approach useful in any way.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard