Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

JavaScript dynamically filter lookup

(0) ShareShare
ReportReport
Posted on by

I am required to update the case quick create form so users enter a category [subjectid (lookup to the subject entity) and a subcategory [cb_subcategory (also a lookup to the subject entity).  When a user enters a category [subject I'd like to dynamically filter the lookup on the subcategory field  [cb_subcategory.  It should only be possible to select subjects where the parent subject is the category. If a user changes the category field then the subcategory should be empty.

When a user enters a category [subjectid the JavaScript below successfully adds a filter to the subcategory field.  On change of subject the subcategory field [cb_subcategory successfully updates to null but the subcategory lookup filter is not updated.  What am I doing wrong?

function setSubjectsFilter(executionContext) {
    formContext = executionContext.getFormContext();
    formContext.getAttribute("cb_subcategory").setValue(null);
    formContext.getControl("cb_subcategory").addPreSearch(filterSubCategory);
    debugger;
}

function filterSubCategory(){
    var parentSubject = (formContext.getAttribute("subjectid").getValue()[0].id).replace("{", "").replace("}", "");
    debugger;
        var fetchXML = "" 
        "" 
          "" 
          "" 
            "" 
          "" 
        ""
      "";
      console.log(fetchXML);
      debugger;
        var layoutXML = "" 
        "                " 
        "                  " 
        "                " 
        "              ";
        var viewID  = "00000000-0000-0000-0000-000000000010";
        var viewDisplayName = "SubCategories";
    if(parentSubject != null) 
    {
        formContext.getControl("cb_subcategory").addCustomView(viewID, "subject",viewDisplayName, fetchXML, layoutXML, true);
    }
    else{
        formContext.getControl("cb_subcategory").setVisible(false);
    }
}

  • Suggested answer
    JeganathanG Profile Picture
    27 on at
    RE: JavaScript dynamically filter lookup

    Hi,

    Here's an example of how you can dynamically filter the "cb_subcategory" lookup field based on the selected "subjectid" value in JavaScript:

    function setSubjectsFilter(executionContext) {
        formContext = executionContext.getFormContext();
        formContext.getAttribute("cb_subcategory").setValue(null);
        formContext.getControl("cb_subcategory").addPreSearch(filterSubCategory);
    }
    
    function filterSubCategory() {
        var parentSubject = (formContext.getAttribute("subjectid").getValue()[0].id).replace("{", "").replace("}", "");
        var fetchXML = ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            "";
        var layoutXML = ""  
            ""  
            ""  
            ""  
            "";
        var viewID = "00000000-0000-0000-0000-000000000010";
        var viewDisplayName = "SubCategories";
        if (parentSubject != null) {
            formContext.getControl("cb_subcategory").addCustomView(viewID, "subject", viewDisplayName, fetchXML, layoutXML, true);
        } else {
            formContext.getControl("cb_subcategory").setVisible(false);
        }
    }
    

    In this code, the setSubjectsFilter function sets the value of the "cb_subcategory" attribute to null and adds a "pre-search" event to the "cb_subcategory" control. The filterSubCategory function filters the data in the "cb_subcategory" lookup field based on the selected value of the "subjected" field.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: JavaScript dynamically filter lookup

    Hi Celal Bey,

    I don't think you did something wrong. I tested your code with Subject entity and I got the same error with yours. The custom view can't be updated. And I also tried addCustomFilter() method and it can't work as well.

    Then I created another entity which has many to one relationship to Subject entity and also create a one to many relationship to case entity. And just changed the properties name in your code, it could work:

    pastedimage1634697751565v1.png

    Change Query to Service:

    pastedimage1634697770936v2.png

    So as a workaround, you could create a custom entity.

    I'm not sure why this code can't work on Subject entity. This could be a bug. I recommend you contact Microsoft Support.

  • SP9 Profile Picture
    25 on at
    RE: JavaScript dynamically filter lookup

    Use formContext as parameter in function definition 

    function filterSubCategory(formContext)

    {

    ............

    }

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,892 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,772 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans