if (typeof Cr5ec === "undefined") { var Cr5ec = {}; } cr5ec_FilteredAvailablePersonnel= { onLoadOrFieldChange: async function (executionContext) { const formContext = executionContext.getFormContext(); const startDate = formContext.getAttribute("cr5ec_startdate")?.getValue(); const endDate = formContext.getAttribute("cr5ec_enddate")?.getValue(); if (!startDate || !endDate) { return; } const startDateISO = startDate.toISOString(); const endDateISO = endDate.toISOString(); try { const fetchXml = ` <fetch> <entity name="cr5ec_projectlabour"> <attribute name="cr5ec_allocatedpersonnel" /> <filter type="and"> <condition attribute="cr5ec_startdate" operator="lt" value="${endDateISO}" /> <condition attribute="cr5ec_enddate" operator="gt" value="${startDateISO}" /> </filter> </entity> </fetch>`; const response = await Xrm.WebApi.retrieveMultipleRecords("cr5ec_projectlabour", "?fetchXml=" + encodeURIComponent(fetchXml)); const busyUserIds = [...new Set(response.entities.map(e => e["cr5ec_allocatedpersonnel"]?.id))].filter(Boolean); const personnelControl = formContext.getControl("cr5ec_allocatedpersonnel"); // Remove old pre-search filters and add a fresh one personnelControl.clearCustomFilter(); personnelControl.addPreSearch(function () { const filterXml = ` <filter type="and"> ${ busyUserIds.length > 0 ? `<condition attribute="cr698_userid" operator="not-in"> ${busyUserIds.map(id => `<value>${id}</value>`).join("")} </condition>` : "" } </filter>`; personnelControl.addCustomFilter(filterXml, "cr698_user"); }); } catch (e) { console.error("Error filtering available personnel:", e.message); } } };
You're on the right track, and your JavaScript is quite close to what you need! Let's walk through a few key improvements and clarifications to help you get this working smoothly in your model-driven app.
Xrm.WebApi.retrieveMultipleRecords
to fetch overlapping assignments.addPreSearch
.The addPreSearch
function should be called before the user opens the lookup. However, you're calling it inside an async
function, which might not complete in time. Instead, you should:
addPreSearch
synchronously with the already-fetched data.clearCustomFilter()
is not a valid method for lookup controls in model-driven apps. You should instead remove and re-add the handler if needed.
The not-in
condition must be properly formatted. Also, ensure that the attribute
in the filter matches the primary key of the cr698_user
entity (likely cr698_userid
).
if (typeof Cr5ec === "undefined") {
var Cr5ec = {};
}
Cr5ec.FilteredAvailablePersonnel = {
onLoadOrFieldChange: async function (executionContext) {
const formContext = executionContext.getFormContext();
const startDate = formContext.getAttribute("cr5ec_startdate")?.getValue();
const endDate = formContext.getAttribute("cr5ec_enddate")?.getValue();
if (!startDate || !endDate) return;
const startDateISO = startDate.toISOString();
const endDateISO = endDate.toISOString();
try {
const fetchXml = `
`;
const result = await Xrm.WebApi.retrieveMultipleRecords("cr5ec_projectlabour", "?fetchXml=" + encodeURIComponent(fetchXml));
const busyUserIds = [...new Set(result.entities.map(e => e["cr5ec_allocatedpersonnel"]?.id))].filter(Boolean);
const personnelControl = formContext.getControl("cr5ec_allocatedpersonnel");
personnelControl.addPreSearch(function () {
let filterXml = `
if (busyUserIds.length > 0) {
filterXml += `
${busyUserIds.map(id => ``).join("")}
`;
}
filterXml += ``;
personnelControl.addCustomFilter(filterXml, "cr698_user");
});
} catch (error) {
console.error("Error filtering personnel:", error.message);
}
}
};
cr698_userid
is the correct schema name for the primary key of the cr698_user
entity.cr5ec_allocatedpersonnel
field is correctly populated in all relevant records.cr698_ProjectPersonnel = true
), your custom filter will combine with it — this is expected and usually fine.🏷️ Tag me if you have any further questions or if the issue persists.
✅ Click "Accept as Solution" if my post helped resolve your issue—it helps others facing similar problems.
❤️ Give it a Like if you found the approach useful in any way.
WarrenBelz
791
Most Valuable Professional
MS.Ragavendar
410
Super User 2025 Season 2
mmbr1606
275
Super User 2025 Season 2