I'm writing a JavaScript function which will be associated to the form of the entity "Collection".
"Collection" has a lookup field to the entity "Director" which shows all directors in the system by default.
"Collection" is also connected to "Movie", which is a related entity.
"Movie" has a lookup field to "director".
So let's say that I'm on the form of "Collection One", which the movies "Taxi Driver" and "Apocalypse now" are related to, and when I click on "Directors" I see all directors saved on my CRM.
What I want to achieve:
Filter the query which retrieves all the directors to see only those which have directed any of the books related to this collection ( "Martin Scorsese" and "Francis Ford Coppola").
What I did so far:
function filterLookup() {
var associatedMovies;
var associatedDirectors;
var targetId = Xrm.Page.getAttribute("collectionid").GetValue();
// retrieved movies associated to this 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 ) {
associatedMovies.push(result.entities[i]);
};
function () {
console.log("error");
}
}
);
// retrieve directors associated to each m in associatedMovies
foreach(m in associatedMovies){
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 ) {
associatedDirectors.push(result.entities[i]);
};
function () {
console.log("error");
}
}
);
}
// filter lookup
// fetchXML to retrieve director connected to the associatedMovies?
}
Can I complete the function and filter the results by using a fetchXML?
I wonder whether I really need those two variables I've been used so far (associatedMovies and associatedAuthors) or if there is a easier way to achieve the task by using a single fecthXML.