Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm writing a JavaScript function for the form of the entity "Collection".
"Collection" has a lookup field to "Director" and is also connected to the related entity "Movie".
"Movie" has a lookup field to "director".
As the lookup shows all directors by default, I want to add a filter to reduce the number of results only to those who directed the movies in the collection.

First of all I need to retrieve the movies in the collection with Web Api but I get an error while running this instruction:

<pre>Xrm.WebApi.retrieveMultipleRecords("movie", "?$select=movieid, directorid,&$filter=collection eq" + targetId).then(
 
            function success(result) {
                for (var i = 0; i < result.entities.length; i++) {
                    movies.push(result.entities[i]);
                }
            },
            function (error) {
                window.alert(error.message);
            }
 
        );


What I have tried:

The complete code:


function filter_Director(executionContext) {

    var formContext = executionContext.getFormContext();
    var targetId = formContext.data.entity.getId();
    var formType = formContext.ui.getFormType();
    var movies;
    var directors;
    
        // retrieve movies in the collection:
        Xrm.WebApi.retrieveMultipleRecords("movie", "?$select=movieid, directorid,&$filter=collectionid eq" + targetId).then(

            function success(result) {
                for (var i = 0; i < result.entities.length; i++) {
                    movies.push(result.entities[i]);
                }
            },
            function (error) {
                window.alert(error.message);
            }
        );

        // retrieve directors linked to at least one of the movies retrieved:
        foreach(m in movies){

            Xrm.WebApi.retrieveMultipleRecords("director", "?$select=directorid, name,&$filter=directorid eq" + m.getAttribute("directorid").getValue).then(

                function success(result) {
                    for (var i = 0; i < result.entities.length; i++) {
                        directors.push(result.entities[i]);
                    };
                    function (error) {
                        window.alert(error);
                    }
                }
            );

        }

        // add filter to lookup
        var filterFetchXml = createFilter();
        formContext.getControl("directorid").addCustomFilter(filterFetchXml);

    }

}



function createFilter() {
   
    var filter =
        "<filter type='and'>" +
        "<condition attribute=directorid operator='in'>";

    foreach(m in movies)
    filter.concat(
        "<value>" +
        m.getAttribute("directorid") +
        "</value>"
    );

    filter.concat(
        "</condition>" +
        "</filter>"
    );

}
Posted
Updated 25-Jan-21 8:13am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900