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

Community site session details

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

get the control of lookup field from an editable grid

(4) ShareShare
ReportReport
Posted on by 10
Hi,
 
I am using an editable grid in Dynamics 365 sales. In this editable grid the fields that are of interest are the optionset field and Lookup field. Lookup value(reference record) has a text field 'market', my motive is to filter the lookup values tied to the specific market when a certain optionset value is selected, else show all lookup values. I performed this successfully in the main form but facing problem implementing the same logic in the editable grid. Looking forward for your suggestions. Thanks. Below is my Js code
 
function filterProdRightsForTerrOnSubgrid(executionContext) {
    var formContext = executionContext.getFormContext();   
    var eventSource = executionContext.getEventSource();
    var gridContext = formContext.getControl("OpportunityProductsGrid");    
    var optionSetValue, lookupControl;
    // Check if the context is a subgrid row
    if (eventSource) {        
        // Get the option set field value 
        var optionSetValue = gridContext.getAttribute("optionsetfield").getValue();
        // Get the lookup control         
        var lookupControl = gridContext.getControl("lookupfield").getControlType(); 
        // Ensure the lookup control exists
        if (lookupControl == null) {
            console.error("Lookup control not found.");
            return;
        }
        // Clear any existing filters
        lookupControl.removePreSearch(function () {
            addLookupFilter(executionContext);
        });
        // Check if option set field has a value that requires filtering
        if (optionSetValue === 802050018 || optionSetValue === 802050240 || optionSetValue === 802050091) {
            lookupControl.addPreSearch(function () {
                addLookupFilter(executionContext);
            });
        }
        else {
            // No filter is applied when the option set value differs
            lookupControl.removePreSearch(function () {
                addLookupFilter(executionContext); // This ensures no filter is applied
            });
            console.log("No option set value or different value selected, showing all lookup values.");
        }
    }
}
function addLookupFilter(executionContext) {
    var formContext = executionContext.getFormContext();
    var gridContext = formContext.getControl("OpportunityProductsGrid"); 
     // Get the lookup field control
    var lookupControl = gridContext.getControl("lookupfield").getControlType();
    // Get the option set field value
    var optionSetValue = formContext.getAttribute("optionsetfield").getValue();
    if (optionSetValue === 802050018 || optionSetValue === 802050240 || optionSetValue === 802050091) {
        // FetchXML to filter the lookup values based on the 'market' field
        var fetchXML = "<filter type='and'>" +
            "<condition attribute='market' operator='eq' value='DACH' />" + // Change 'DACH' as needed
            "</filter>";
        lookupControl.addCustomFilter(fetchXML, "lookupEntity");
    }
    else {
        // Apply an empty filter to show all records (no actual filtering)
        var emptyFilter = "<filter type='and'></filter>";
        lookupControl.addCustomFilter(emptyFilter, "lookupEntity");
    }
}
I have the same question (0)
  • Verified answer
    Hamza H Profile Picture
    1,787 on at
    get the control of lookup field from an editable grid
    Editable grids in Dynamics 365 don’t support JavaScript-based lookup filtering, unlike main forms. Your code won’t work as expected in the grid. To achieve filtering, you’ll need a custom PCF control or consider using a quick view or custom UI as a workaround.
     

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daniyal Khaleel Profile Picture

Daniyal Khaleel 147

#2
DAnny3211 Profile Picture

DAnny3211 134

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 70 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans