How to Filter Lookup based on another Lookup value using javascript in Dynamics 365?

Scenario:

Here we have IP Admission, Lab Indent lookups on IP Service Entity. If we want to filter Lab Indent lookup based on IP Admission value, then the following steps will help us.

Using the if condition, we must first determine whether the IP Admission lookup is null or not.


Lookup
If IP Admission lookup value is null, then Lab Indent lookup will show all available Lab indents.
Lookup1

Else show the Lab Indent lookup filter on IP Admission Value.

Lookup2

To filter lookup, get the GUID of IP Admission(ipadmissionID) and set it in filter condition assigned to filter variable.

addPreSearch() function is used to get the records of lab Indent which satisfy the condition provided in addCustomFilter() function. Here addPresearch() takes another function as parameter.

Through filter variable we are defining the custom filter condition. Pass the custom filter condition as parameter to addCustomFilter() function.
Lookup3

Front End of Dynamics:

Here is the list of Lab Indent records with corresponding IP Admissions.
Lookup4

Once admission number IP22-0334 is selected we can filter lab indent lookup. If we click on magnifying glass of Lab Indent lookup, we will see lab indent records (IND-00040, IND-00041, IND-00042) related to IP22-0334.

Lookup

Note:
         Using this JavaScript, we can filter lookup on load of the form.

Alternative Approach:

If we need to filter Lab Indent lookup on Change of IP Admission lookup, then we can use the following configuration process.
Lookup6

  1. Open Main Form in PowerApps and select Lab Indent Lookup.
  2. In the properties of field set the filter criteria.
  3. Check Filter by related rows Check Box

  4. In the Relationship to current table Optionset, we must select IP admission lookup on IP Service and in Relationship to this lookup’s table, select the IP Admission Lookup on Lab Indent.

    Code:

    function labIndentLookupfilter(executionContext)

        {

            debugger;

            var formContext = executionContext.getFormContext();

            if (formContext.getAttribute(‘neo_ipadmission’).getValue() == null)

            {

                formContext.getControl(“neo_labindent”).setVisible(false);

            }

            else

            {

                var ipadmissionID = formContext.getAttribute(‘neo_ipadmission’).getValue()[0].id.slice(1, -1);

                formContext.getControl(“neo_labindent”).setVisible(true);

                formContext.getControl(“neo_labindent”).addPreSearch(function ()

                {

                    var filter = “<filter> <condition attribute=’neo_admissionno’ operator=’eq’ value='{” + ipadmissionID + “}’/></filter>”;

                    Xrm.Page.getControl(“neo_labindent”).addCustomFilter(filter);

                });

                formContext.getAttribute(“neo_labindent”).setValue(null);

            }

     }

 

Leave a Reply