Lookup Control PreSearch Event is the new feature in CRM 2013 and we can simply use it to apply the filter immediately when the lookup is clicked.
The lookup control has a PreSearch event that occurs just before the control launches a dialog to search for records. Unlike other form events, the Form Editor does not provide a UI to set event handlers for this event. We must use the addPreSearch and removePreSearch methods on the lookup control to add or remove event handlers for this event. Use this event with other Lookup Control Methods to change the results displayed in a lookup based on the form data current just before the lookup control shows search results for a user to choose from.
Use addPreSearch method to apply changes to lookups based on values current just as the user is about to view results for the lookup.
JavaScript
Xrm.Page.getControl(arg).addPreSearch(handler)
Sample of JavaScript:
// Trigger(s): onLoad form
// Description: Lookup to filter the Verification Tracking Information records
function preFilterLookup() {
Xrm.Page.getControl(“test_verificationdocument”).addPreSearch(function () { addLookupFilter(); });
}
// Description: Put your custom filter condition
function addLookupFilter() {
var id = Xrm.Page.data.entity.getId();
if (locmanageID != null) {
fetchXml = “”;
Xrm.Page.getControl(“dodd_verificationdocument”).addCustomFilter(fetchXml);
}
}
Add function on load of the form:
I hope you find it useful!
Author: Zhe Chen
Title: Lead Dynamics CRM Consultant @ Adisys
Email: zhechen@adisys.co