I'm trying to build out a flexible, custom search engine as a component, but am stuck. I want to be able to specify filter fields in a comma delimited input attribute (strFilterFields
in my code) and have each of those show up as filterable apex:inputField
inputs. As of now, I'm using a generic new sObject
so that the input fields take on the characteristics they should (text vs picklist vs lookup, etc) but I'm only able to use this methodology for "first level" fields. Anybody know of a way to allow for this dynamic view while allowing for lookup relationships? For instance, I'd love to be able to search Opportunities by Account.ParentId
Notice querySObject
Here is a dumbed-down version of my visualforce component:
<apex:component controller="ComponentRelatedList" id="theController">
<apex:attribute type="String" name="componentId" required="true" description="The unique Id of this component" />
<apex:attribute type="String" name="sObjectName" required="true" assignTo="{!strSObjectName}" description="The name of the object you intend to query" />
<apex:attribute type="String" name="recordTypeName" required="false" assignTo="{!strRecordTypeName}" description="The name of the RecordType you intend to filter to" />
<apex:attribute type="String" name="filterFields" required="false" assignTo="{!strFilterFields}" description="The fields you are allowing the user to tweak" />
<apex:form id="theForm">
<apex:repeat value="{!filters}" var="filter">
<apex:inputField required="false" value="{!querySObject[filter]}" />
</apex:repeat>
<apex:commandButton value="Go!" />
<apex:dataTable>
... dataTable contents here, not relevant for this discussion...
</apex:dataTable>
</apex:form>
</apex:component>
And here is a dumbed-down version of my apex controller:
public class ComponentRelatedList {
public String strSObjectName { get; set; }
public String strRecordTypeName { get; set; }
public String strFilterFields { get; set; }
public sObject querySObject {
get {
if (querySObject == null) {
querySObject = Schema.getGlobalDescribe().get(strSObjectName).newSObject();
if (isFilteredRecordType) querySObject.put('RecordTypeId', UtilsVariables.getRecordTypeId(strSObjectName, strRecordTypeName));
}
return querySObject;
}
set;
}
}
And here is a snapshot of the output (notice the correct handling of each Field Type [red arrows]):