Pre-Filtering Multi-table Lookups
A very common requirement which is being request frequently is having a lookup table that points to multiple tables similar to the out of the box customer lookup type that can point out to either a contact or account.
This feature was released in July 2021 but I hadn’t the chance to try it till now and it came up with a challenge to using it which is Prefiltering this new type of lookup and I have searched and googled and everything and I really could not find someone who tried it, Many articles pointing out how to create this column typeand how it looks like but none really explained how to add prefiltration so it does not display all the records but show them as filtered based on a predefined criteria.
This article by Nick Doelman has a very good explanation to Polymorphic lookups.
This article describes how to do pre-filtration to a normal lookup type.
Also with the help of the XRMToolbox Polymorphic Lookup Creator it was very easy to create the polymorphic lookup which for the sake of the example below my lookup needed to point to either contact or user tables with a pre-search filter where first name = Mira
SO when it came to the prefiltration it was pretty easier than I thought and I just tried it and to my Surprise it worked, so all I did is used the normal way for lookup filtration but added a custom filter for each of my tables.
function FilterPolymporphicLookup(executionContext){
formContext = executionContext.getFormContext();
formContext.getControl("new_polymorphiclookupid").addPreSearch(filterPolymorphic);
}
function filterPolymorphic() {
var contactFilter = "<filter type='and'><condition attribute='firstname' operator='eq' value='Mira' /></filter>";
var userFilter = "<filter type='and'><condition attribute='firstname' operator='eq' value='Mira' /></filter>";
formContext.getControl("new_polymorphiclookupid").addCustomFilter(contactFilter, "contact");
formContext.getControl("new_polymorphiclookupid").addCustomFilter(userFilter, "systemuser");
}Multilookup
![]()
miraghaly
![]()
