I want to filter the lookup on multiple fields on one form. The problem is that only the last eventhandler I added applies to all fields. For this I wanted to use the context to get the field, which it is currently used. But the filters are applied in the "onload" event of the form, where I don't have the selected context/field in it.
For this I use this code:
function preFilterLookupFunction(executionContext)
{
var fieldname = new Array("field1","field2");
fLen = fieldname.length;
for (i = 0; i < fLen; i++)
{
var tempName = fieldname[i];
Xrm.Page.getControl(tempName).addPreSearch(function ()
{
addLookupFilterFunction(executionContext);
});
}
}
function addLookupFilterFunction(executionContext)
{
var attribute = executionContext.getEventSource();
var tempFieldName = attribute.getName();
alert(tempFieldName)
var condition = Xrm.Page.getAttribute("fieldContition").getValue();
if (condition != null)
{
var fetchXml = "<filter type='and'><condition attribute='fieldContition' operator='eq' value='" + condition + "' /></filter>";
Xrm.Page.getControl(tempFieldName).addCustomFilter(fetchXml);
}
}
My question: is it possible to get the context in the addLookupFilterFunction while executing, so I can set the filter to the right field? Or is there a better way to filter multiple field on one form?
The key is that you do not need to pass an executionContext parameter.
In function call
addLookupFilterFunction(executionContext)executionContext references the argument variable of functionpreFilterLookupFunction(executionContext), which will be the same for every iteration of theforloop.Replace this code:
by this: