Recently I faced a strange issue, I used a filed (lookup) twice in the form and applied a pre-filtering java-script to the field. But only one filed is getting filtered by the script.I tried many options and I couldn’t identify the issue. Have you even faced this issue ? Finally I got the answer from Scott Durow ( Thank you Scott!!! ). I decided to post this issue because if someone is facing the same issue , this will help them to solve the issue. Following is the explanation for this issue.
The issue you are experiencing is because the ‘getControl’ method returns only the first control – so in fact you are only adding the custom filter to the first control.
An attribute object has a ‘controls’ collection that can give you all the controls for a specific attribute. So you can add the filter to all the controls by using something like:
Xrm.Page.getAttribute(“fieldname”).controls.forEach(
function (control, i) {
control.addPreSearch(filterCustomerAccounts);
var customerAccountFilter = “<filter type=’and’><condition attribute=’accountcategorycode’ operator=’eq’ value=’1’/></filter>”;
control.addCustomFilter(customerAccountFilter, “account”);
}
);
Hope this helps!!!
Nijo