Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Filter lookup value based on another column using Javascript addPreSearch in the form not working

Posted on by 2
In dataverse, I have opportunity table with a lookup filed named subject and a option set field named casetype which have 2 values user can choose from. I need to show different values in the subject lookup based on the case type user chooses. I made a onchange event handler on case type field in the form and add this code for it. FOr some reasons, its not working. The control is not event going in the AddSubjectFilter function from where I setting my filters and calling the addCustomFilter api on subjects table.
If any one has recently worked with this type of problem then kindly assist, as this code should have worked earlier but looks like Microsoft has changed things and its not working now
In console I only get the console log for 
console.log("Subject field : ", formContext.getControl("subjectid")); and
 console.log("PreSearch added to subject lookup.");
 
function filterSubjectOptions(executionContext) {
 
    var formContext = executionContext.getFormContext(); // Get the form context
  
    console.log("Subject field : ", formContext.getControl("subjectid")); 

    //formContext.getControl("subjectid").addPreSearch(AddSubjectFilter); // not working
    formContext.getControl("subjectid").addPreSearch(function() {
        AddSubjectFilter(formContext); // Pass formContext directly still not working
    });

    console.log("PreSearch added to subject lookup."); // Verify that addPreSearch was called
}

function AddSubjectFilter(formContext) {
    console.log("CustomFilterFunction triggered");


    var caseTypeAttribute = formContext.getAttribute("casetypecode"); // Get the Case Type field
    if (!caseTypeAttribute) {
        console.error("Case Type attribute is null or undefined.");
        return; // Exit if Case Type is not found
    }

    var caseType = caseTypeAttribute.getValue(); // Get the value of the Case Type
    if (caseType == null) {
        console.error("Case Type is not selected.");
        return; 
    }

    console.log("Case Type selected: ", caseType); // Log the Case Type value

    var filterXml = ""; 

    // Apply the filter based on the selected Case Type
    if (caseType == 1) { // Case Type: Service Request
        filterXml = "<filter type='or'>" +
            "<condition attribute='title' operator='eq' value='Increase Usage Limit' />" +
            "<condition attribute='title' operator='eq' value='Request Secondary Card' />" +
            "</filter>";
    } 
     if (caseType == 2) { // Case Type: Complaint
        filterXml = "<filter type='or'>" +
            "<condition attribute='title' operator='eq' value='Amount not reimbursed' />" +
            "<condition attribute='title' operator='eq' value='Card charges applied incorrectly' />" +
            "</filter>";
     }

    // Apply the custom filter to the Subject lookup field
    formContext.getControl("subjectid").addCustomFilter(filterXml,'subject');
   
}

 
 
  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 7,590 Super User 2024 Season 1 on at
    Filter lookup value based on another column using Javascript addPreSearch in the form not working
    Hello User,
     
    Try to check your syntax and compare it with the MS docs. Make sure run small function just for the testing purpose.
     
     
    Thank you,
    Amit katariya

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans