Skip to main content

Notifications

Announcements

No record found.

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

Lookup filtering not working for dynamically created fetch xml for addfilter function

Posted on by 676

Hi guys,

I am using the presearch and addfilter function in a combination. Everything seems to work fine when I use a hardcoded filter like below:

function addPrimaryTagLookupFilter(executionContext) {

    var filter =        "<filter type='and'>" +
    "<condition attribute='tco_category' operator='in'>" +
    "<value>EADD077E-B0DA-E911-A982-000D3ABA0C96</value>" +
    "<value>ECDD077E-B0DA-E911-A982-000D3ABA0C96</value>" +
    "<value>E6DD077E-B0DA-E911-A982-000D3ABA0C96</value>" +
    "</condition>" +
    "</filter>"
    
    var formContext = executionContext.getFormContext();
    formContext.getControl("tco_primary_tag").addCustomFilter(filter);        

}
But when I try creating the same filter dynamically, it doesn't seems to work. 
                Xrm.WebApi.online.retrieveMultipleRecords("tco_solution_setting""?$select=tco_value&$filter=tco_name eq '" + solutionSettingKey + "'").then(
                    function success(solutionSettingResults) {
                        if (solutionSettingResults.entities.length > 0) {
                            for (var i = 0i < solutionSettingResults.entities.lengthi++) {
                                var tco_value = solutionSettingResults.entities[i]["tco_value"];
                                var requiredFields = tco_value.split(';');
                                // var filter = "<filter type='and'>" + "<condition attribute='tco_category' operator='in'>"
                                for (var j = 0j < requiredFields.lengthj++) { 
                                    if (requiredFields[j!= '') {                                                                            
                                            debugger;
                            filter += "<condition attribute='tco_category' operator='eq' value='" + '{' + requiredFields[j+ '}' + "'/>";

                            //  filter += "<value>" + requiredFields[j]  + "</value>";
                                        }
                                }
                                filter += '</filter></filter>';
                            }
                            // var formContext = executionContext.getFormContext();
                        
                            formContext.getControl("tco_primary_tag").addCustomFilter(filter);    
                        }
                    },
I have also tried to use the following code as suggested in the comments, but still it doesn't seems to work.
Xrm.WebApi.online.retrieveMultipleRecords("tco_solution_setting""?$select=tco_value&$filter=tco_name eq '" + solutionSettingKey + "'").then(
                    function success(solutionSettingResults) {
                        if (solutionSettingResults.entities.length > 0) {
                            for (var i = 0i < solutionSettingResults.entities.lengthi++) {
                                var tco_value = solutionSettingResults.entities[i]["tco_value"];
                                var requiredFields = tco_value.split(';');
                                var filter = "<filter type='and'>" + "<condition attribute='tco_category' operator='in'>"
                                for (var j = 0j < requiredFields.lengthj++) { 
                                    if (requiredFields[j!= '') {                                                                            
                                            debugger;
                             filter += "<value>" + requiredFields[j]  + "</value>";
                                        }
                                }
                                filter += '</condition></filter>';
                            }
                            var formContext = executionContext.getFormContext();
                        
                            formContext.getControl("tco_primary_tag").addCustomFilter(filter);    
                        }
                    },
There is no error in the console. But the lookup doesn't gets filtered. Any suggestions guy why this couldn't be working? It's urgent actually. I am working on UCI.
  • Mohd Tahir Profile Picture
    Mohd Tahir 676 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Joana Pinto

    1 - What happens when you debbug? I see a breakpoint there, does the code hit it?

    ---> It hits, the debugger.

    2 - What is inside "requiredFields" array? My guessing is that it does not enter on that FOR iteration because maybe that split isn't correct.

    ---> No it is. I have debugged the loop from end to end.

  • Joana Pinto Profile Picture
    Joana Pinto 740 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Hi,

    1 - What happens when you debbug? I see a breakpoint there, does the code hit it?

    2 - What is inside "requiredFields" array? My guessing is that it does not enter on that FOR iteration because maybe that split isn't correct.

    Regards,

    Joana

  • Suggested answer
    ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    In your above code you missed to close the condition tag. This might be the problem

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Could you please share  debugging screenshot with the value  in add watch window here.

  • Mohd Tahir Profile Picture
    Mohd Tahir 676 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    The filter query is correct. I have already tested it. No problem with that.

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Hi Mohd ,

    Use CRMRestBuilder to generate your query , seems the problem is  not getting correct value while filtering. You can try to debug and check whether value you are adding in the filter criteria are  correct or not.

    www.youtube.com/watch

  • Kirsh Jay Profile Picture
    Kirsh Jay 70 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Try if this works.

    Xrm.WebApi.online.retrieveMultipleRecords("tco_solution_setting", "?$select=tco_value&$filter=tco_name eq '" + solutionSettingKey + "'").then(

           function success(solutionSettingResults) {

               if (solutionSettingResults.entities.length > 0) {

                   for (var i = 0; i < solutionSettingResults.entities.length; i++) {

                       var tco_value = solutionSettingResults.entities[i]["tco_value"];

                       var requiredFields = tco_value.split(';');

                       var filter = "<filter type='and'>" + "<condition attribute='tco_category' operator='in'>"

                       for (var j = 0; j < requiredFields.length; j++) {

                           if (requiredFields[j] != '') {

                               filter += "<value>" + requiredFields[j] + "</value>";

                           }

                       }

                       filter += '</condition></filter>';

                   }

                   // var formContext = executionContext.getFormContext();

                   formContext.getControl("tco_primary_tag").addCustomFilter(filter);

               }

           });

    Thanks!

    Kirsh

  • Suggested answer
    ajyendra Profile Picture
    ajyendra 1,730 on at
    RE: Lookup filtering not working for dynamically created fetch xml for addfilter function

    Hi,

    I don't find something unusual on your code but this minor change helps

    var filter = "<filter type='and'><condition attribute='tco_category' operator='in'>"

                for (var j = 0; j < requiredFields.length; j++) {

                     if (requiredFields[j] != '') {                                                                            

                           debugger;

                           filter += "<value>" + requiredFields[j]  + "</value>";

                    }

                }

    filter += '</condition></filter>';

    Hope it helps

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans