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;
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);
}
);
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);
}
}
);
}
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>"
);
}