Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Java Script to filter records via Choice column (multiselect)

Posted on by
Hi there,
 
I need to build a Java Script for my Model Driven App. I don't know JS, but I use online resources, Copilot and ChatGpt and usually I am able to build something that works. Not this time and even though I invested so much time in this, I just can't seem to figure it out.
 
Tables Context:
 
1. Table Pachet Vacanta (pv_pachetvacanta). Has a multi select choice column (global column) called Categorie (pv_categorie).
2. Table Termeni si Conditii (pv_termenisiconditii). Has the same multi select choice column with the same name.
3. Table Termeni si Conditii Pachet Vacanta (pv_termenisiconditiipachetvacanta)
- With look-up column to Pachet Vacanta (pv_pachetvacanta)
- And look-up column to Termeni si Conditii (pv_termenisiconditii).
-This last table is added as a subgrid in my main form in Pachet Vacanta.
 
What I need to achieve:
 
In my main form within Termeni si Conditii Pachet Vacanta, filter the records in Termeni si Conditii to only display those that have the same choice selections as the current Pachet Vacanta. If in Pachet Vacanta I have selected the choices A, D, Z, I should see all of the records in Termeni si Conditii which have these choices assigned (whether it's independently or in combination with other choices).
 
Here is the code that I have at the moment (Pachet Vacanta look-up column is read only, so since it does not change, the Event is on onLoad, instead of onChange). My current code works if I have only one choice in Pachet Vacanta. The moment I add an additional one, it breaks, even though I can see that it still detects the value of my choices.
 
function filterTermeniPachetVacanta(executionContext) {
    var formContext = executionContext.getFormContext();
    var pachetVacantaId = formContext.getAttribute("pv_pachetvacanta").getValue();
    var termeniControl = formContext.getControl("pv_termenisiconditii");

    if (!pachetVacantaId) {
        console.error("Pachet Vacanta is not selected.");
        return;
    }

    // Fetch categories from the selected Pachet Vacanta
    Xrm.WebApi.retrieveRecord("pv_pachetvacanta", pachetVacantaId[0].id, "?$select=pv_categorie").then(
        function success(result) {
            var categories = result["pv_categorie"];
            if (!categories) {
                console.error("No categories found or data is not in the expected format.");
                return;
            }

            // Ensure categories is an array
            categories = Array.isArray(categories) ? categories : [categories];
            console.log("Retrieved categories:", categories);

            // Generate filter XML with each category in a separate <condition> tag using the eq operator
            var categoryConditions = categories.map(function (category) {
                return "<condition attribute='pv_categorie' operator='eq' value='" + category + "' />";
            }).join("");

            var filterXml = "<filter type='or'>" + categoryConditions + "</filter>";

            console.log("Generated Filter XML for Termeni si Conditii:", filterXml);

            // Apply filter to the Termeni si Conditii lookup
            if (termeniControl) {
                termeniControl.addPreSearch(function () {
                    termeniControl.addCustomFilter(filterXml);
                });
                console.log("Filter applied to Termeni si Conditii lookup.");
            } else {
                console.error("Termeni si Conditii lookup control not found.");
            }
        },
        function (error) {
            console.error("Error retrieving Pachet Vacanta categories:", error.message);
        }
    );
}

function onLoad(executionContext) {
    filterTermeniPachetVacanta(executionContext);
}

Here is my error when I have multiple choices added in Pachet Vacanță

 

 
 
Thank you in advance for any help or suggestions! If you have any other ideas on how I can implement this without a JS, let me know. So far this was the only approach I could find. Many thanks again!

 

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans