Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

get the control of lookup field from an editable grid

(1) ShareShare
ReportReport
Posted on by 4
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");
    }
}

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,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans