I already tried with the IN operator, same result, the grid record is not showing in the lookup.
if (FormContext.getAttribute("dummy_inspector").getValue() != null) {
var inspectorId = FormContext.getAttribute("dummy_inspector").getValue()[0].id;
var inspectorType = FormContext.getAttribute("dummy_inspector").getValue()[0].entityType;
var condition = "";
if (inspectorType.toLowerCase() == "account") {
// Get accountid(s) from inspection sites
var inspectionsitesGrid = FormContext.getControl("listInspectionSites").getGrid();
var inspectionsitesRows = inspectionsitesGrid.getRows();
var rowCount = inspectionsitesGrid.getTotalRecordCount();
for (var i = 0; i < rowCount; i++) {
var rowEntity = inspectionsitesRows.get(i).getData().getEntity();
condition += "<value>" + rowEntity.getKey() + "</value>";
}
}
var lookupControl = FormContext.getControl("dummy_inspectionlocation");
if (lookupControl != null) {
var customerAddressFilter = "<filter type='and'><condition attribute='dummy_customer' operator='in'><value>" + inspectorId + "</value>" + condition + "</condition></filter>";
lookupControl.addPreSearch(function () { lookupControl.addCustomFilter(customerAddressFilter); });
}
}
But, I know the reason why the grid records were missing in the lookup.
The rowcount ALWAYS was 0.
So I placed this function in my onload:
getTotalGridRecordCount: function() {
debugger;
var functionName = "onLoad";
try {
setTimeout(function () {
if (Xrm.Page != null && Xrm.Page != undefined && Xrm.Page.getControl("listInspectionSites") != null && Xrm.Page.getControl("listInspectionSites") != undefined) {
var count = Xrm.Page.getControl("listInspectionSites").getGrid().getTotalRecordCount();
SetLookupFilter();
}
}, 5000);
}
catch (e) {
Xrm.Utility.alertDialog(functionName + "Error: " + e.message || e.description);
}
},
With SetLookupFilter having the code from earlier on.
So, after 5 seconds the grid including the records were loaded so I could build the lookupfilter.
BUT ! Then I got a second problem, the grid records were still missing in the lookup, despite the fact that the lookup filter was containing it.
Reason for this is that when the lookup filter is loaded, a override of it is not working. It's not refreshing the filter of the lookup.
So what have I done?
I changed my code so that in case of a onload I wait for 5 seconds so I'm sure the grid is loaded including the records, and then my lookup filter was containing these records.
And when I add records to the N:N subgrid, I reload my form, so 5 seconds later my lookup filter is containing that new record(s).