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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Polymorphic Lookup Filter

(1) ShareShare
ReportReport
Posted on by 8
Hello guys,
 
I have a quick create form with two fields (A and B)
Field A = Option Set (values: account and user)
Field B = Polymorphic Lookup (systemusers and contacts)

I need to create a filter inside Field B where:
1- If field A is with the value "user", add a filter to field B showing only System Users
2- If field A is with the value "account", add a filter showing only contacts that are related to an account (I have this account ID to pass inside the fetch)

I tried this (dynamicFunction was added "addOnChange" of field A:
 
dynamicFilter: async function (executionContext) {
 
    const formContext = typeof executionContext.getFormContext === "function" ? executionContext.getFormContext() : executionContext;
    const lookupControl = formContext.getControl("user_contact_id");
 
    if (preSearch) lookupControl.removePreSearch(addFilter);
    lookupControl.addPreSearch(addFilter);
 
    async function addFilter() {
        const signAs = formContext.getAttribute("sign_as")?.getValue();
        if (preSearch) lookupControl.removePreSearch(addFilter);
 
        if (!signAs) {
            lookupControl.addCustomFilter(`<filter><condition attribute='contactid' operator='eq' value='00000000-0000-0000-0000-000000000000' /></filter>`);
        } else if (signAs == signAsUser) {
            var filterXmlUser = `<filter><condition attribute='systemuserid' operator='neq' value='00000000-0000-0000-0000-000000000000' /></filter>`;
            lookupControl.setEntityTypes(["systemuser"]);
            lookupControl.addPreSearch(() => {
                lookupControl.addCustomFilter(filterXmlUser);
            });
        } else if (signAs == signAsAccount) {
            const record = formContext.getAttribute("related_record")?.getValue()[0]?.id;
            const result = await Xrm.WebApi.online.retrieveRecord("recordX", record, "?$select=_counterpart_value");
 
            if (result) {
                const counterpart = result._counterpart_value;
 
                if (counterpart) {
                    const fetchXml = `
                        <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
                            <entity name="contact">
                                <attribute name="contactid"/>
                                <link-entity name="edpb2c_account_contact" from="contactid" to="contactid" visible="false" intersect="true">
                                    <link-entity name="account" from="accountid" to="accountid" alias="ac">
                                        <filter>
                                            <condition attribute="accountid" operator="eq" uitype="account" value="${counterpart}"/>
                                        </filter>
                                    </link-entity>
                                </link-entity>
                            </entity>
                        </fetch>`;
 
                    const contacts = await Xrm.WebApi.retrieveMultipleRecords("contact", `?fetchXml=${encodeURIComponent(fetchXml)}`);
 
                    if (contacts?.entities?.length) {
                        const contactIds = contacts.entities.map(contact => `<value>${contact.contactid}</value>`).join("");
                        const filterXml = `<filter><condition attribute='contactid' operator='in'>${contactIds}</condition></filter>`;
 
                        lookupControl.setEntityTypes(["contact"]);
                        lookupControl.addPreSearch(() => lookupControl.addCustomFilter(fetchXml, "contact"));
                    }
                }
            }
        } else {
            lookupControl.addCustomFilter(`<filter><condition attribute='contactid' operator='eq' value='00000000-0000-0000-0000-000000000000' /></filter>`);
        }
 
        preSearch = true;
    }
}

 
I'm having some problems like:
- When I change field A, from user to account, I need to click on search button twice (only one, the filter stays loading)
- When I change field A, from user to account, and then, from account to user again, the filter stops working

What can I change? Or if you guys have a different way to create this filter, I would like to learn.
Thanks very much!
I have the same question (0)
  • Ray Profile Picture
    1,537 on at
    I think the code line is duplicate:
    lookupControl.addPreSearch(() => lookupControl.addCustomFilter(fetchXml, "contact"));
    It adds a new presearch function to the control.

    And you do not need to call lookupControl.addPreSearch in function addFilter , just call lookupControl.addCustomFilter is enough.

    And the counterpart is related to a field, you can get the value outside the addFilter function to simple the logic.


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans