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 :
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);
    }
}

I have the same question (0)
  • SP9 Profile Picture
    25 on at

    Use formContext as parameter in function definition 

    function filterSubCategory(formContext)

    {

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

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    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.

  • Suggested answer
    JeganathanG Profile Picture
    27 on at

    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.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
iampranjal Profile Picture

iampranjal 41

#2
Martin Dráb Profile Picture

Martin Dráb 36 Most Valuable Professional

#3
Satyam Prakash Profile Picture

Satyam Prakash 35

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans