If you have been using Salesforce Lightning Experience, you probably are familiar with the Lightning Polymorphic Lookup. For example, you will see it while creating Tasks, in the "Name" and "Related to" fields:
Basically, you get to chose the "type" of SObject; when you type text, a search is performed on the appropriate SObject.
I was tasked with building a Lightning page where I needed similar functionality with some finer tuning:
I wrote a general purpose Polymorphic Lookup that would perform a custom query to choose the records that a user is allowed to select from. The source code can be found here: https://github.com/veenasundara/Lightning-Polymorphic-Lookup
I will not describe the details of the component itself, but rather how you would use it. So, here is the how I used it:
<aura:component controller="CmpPolyLookupExampleCtrl"><aura:attribute name="selectedTeamMemberId" type="String" access="private"/>
<aura:attribute name="selectedTeamMemberName" type="String" access="private"/>
<aura:attribute name="selectedType" type="String" default="Users" access="private"/><c:CmpPolymorphLookup lookupLabel="Add Team Members"
selectedMenuType="{!v.selectedType}"
selectedId="{!v.selectedTeamMemberId}"
selectedName="{!v.selectedTeamMemberName}" ><c:CmpPolymorphLookupType type="Users"
fetchRecordsMethod="{!c.getUsers}"
iconType="standard"
iconName="user">
</c:CmpPolymorphLookupType><c:CmpPolymorphLookupType type="User Groups"
fetchRecordsMethod="{!c.getUserGroups}"
iconType="standard"
iconName="groups">
</c:CmpPolymorphLookupType></c:CmpPolymorphLookup>
</aura:component>
The code above looks like this:
Here's what selecting "User" does:
And this happens when "User Groups" is selected:
The component CmpPolymorphLookup takes 4 attributes; its "body" contains one CmpPolymorphLookupType component for each "type" that a user can choose from.
CmpPolymorphLookup's attributes are:
aura:handler name="change" value="{!v.selectedTeamMemberName}" action="{!c.TeamMemberSelected}"/>
Since I have 2 "types" (Users and User Groups), you see 2 CmpPolymorphLookupType components in the body of the CmpPolymorphLookup component.
CmpPolymorphLookupType takes 4 attributes:
This solution's flexibility comes from being able to specify fetchRecordsMethod. For each "type", we will have to create one method in our Apex Controller (CmpPolyLookupExampleCtrl in my case). This method will perform a custom query and return a List<List<String>>.
The inner List will contain 2 members for each record we want to return (key, value). When returning SObject records, this could be the record's Id and Name. For picklists, it could be the value and label.
In my Apex Controller, CmpPolyLookupExampleCtrl, my getUsers and getUserGroups methods look like this:
//
//CmpPolyLookupExampleCtrl.cls
//
@AuraEnabled
public static List<List<String>> getUsers()
{
User[] users = [Select Id, Name From User
Where UserType = 'Standard' and
IsActive = true and LastName != 'Site Guest User'
order by Name];
List<List<String>> records = new List<List<String>>();records.add(new List<String>{'All', '--All Users--'});
for(User u : users)
{
records.add(new List<String>{u.Id, u.Name});
}return records;
}@AuraEnabled
public static List<List<String>> getUserGroups()
{Schema.DescribeFieldResult fieldResult = User.User_Group__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();List<List<String>> records = new List<List<String>>();
Map<String, Schema.PicklistEntry> mapPLEntryByLabel = new Map<String, Schema.PicklistEntry>();
for( Schema.PicklistEntry f : ple)
{
mapPLEntryByLabel.put(f.getLabel(), f);
}
List<String> lstLabels = new List<String>(mapPLEntryByLabel.keySet());
lstLabels.sort();records.add(new List<String>{'All', '--All User Groups--'});
for(String label: lstLabels)
{
Schema.PicklistEntry pe = mapPLEntryByLabel.get(label);
records.add(new List<String>{pe.getValue(), pe.getLabel()});
}return records;
}
Note: The component I have written "requires" a custom query. Feel free to modify it to allow for a "default" behavior that will simply select the Id and Name of an SObject with no specific filters. Also, please post below if you find any other ways to customize this component!
See what else you can do with the Customizable Polymorphic Lookup Component. We can help with our custom development services.