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,...
Answered

Problem with addCustomFilter() to filter lookup field

(0) ShareShare
ReportReport
Posted on by 448

Hi all

First, I'm completly new with javascript .....

I try to add a custom filter to my lookup field and I've copied this function from Microsoft Learn (https://learn.microsoft.com/it-it/power-apps/developer/model-driven-apps/clientapi/reference/controls/addcustomfilter).

The script is inserted in OnLoad event of page and work until red line, without giving errors (I put some popup to know where ) ... can somebody explain me where I'm mistaken?

pastedimage1672764188898v1.png

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    The answer is - you declare a function but you don't use it. Check this post - it gives a better explanation of how the filtering works - www.inogic.com/.../

  • Suggested answer
    CRMJetty Profile Picture
    3,512 on at

    Hello DiePic 

     I made a custom entity and I'm trying to get the name or id or anything that could work of an account from a lookup, and to apply a filter with that account on another lookup on opportunities, it means advance filter in lookup. below code i have done and its working for me.

    Try with this -

    function OnChangeOfAccount() {
    Xrm.Page.getControl("opportunityLookpField").addPreSearch(function () {
    addLookupFilter();
            });

    function addLookupFilter() {
    if (XRM.Page.getAttribute("Accountlookuofield") != null) {
    var account = Xrm.Page.data.entity.attributes.get("Accountlookuofieldt").getValue()[0].id; // Xrm shhoud be smalll not like XRM
    if (account != null) {
    fetchXml = "<filter type='and'><condition attribute='parrentaccountid' operator='eq' value='" + account + "' /></filter>";
    Xrm.Page.getControl("opportunityLookpField").addCustomFilter(fetchXml);
                   }
           }
    }

    I hope it helps,
    Thanks.
    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Portal.

  • DiePic Profile Picture
    448 on at

    Thanks for reply a33ik  and Maulik Shah 
    Your suggestion had make me find new material for my studies.

    Now I've this script that apparently work well, but when I select the lookup field and press ENTER, the list is complete ... not filtered.

    For all the rest i's working well. 

    Can you help me to correct this error?


    pastedimage1672840161644v1.png

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Replace this line

    pastedimage1672839109641v1.png

    with the line

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

  • DiePic Profile Picture
    448 on at

    Hi a33ik 

    Your suggestion make me discover more concepts...

    I've changed a little my script because I want that filter is starting every time that I press enter on lookup field.

    Now the list is always complete (not filtered) and give me this error.

    Can you help me more?

    pastedimage1672841629979v1.png

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Remove the "order" part - it shouldn't be there

    pastedimage1672841814617v1.png

  • DiePic Profile Picture
    448 on at

    Thanks a lot a33ik 

    now it's working well !!! 

    Please only for my knowledge, there is a way to insert the string XML with order or not?

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    There is no way, as far as I know. It's possible to use only filters/conditions inside - so basically that's related to filtering.

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