I need to build an interface to allow a user to filter record by the field chosen by the user. I know that I can use Text Area
fields in the WHERE
Clause but I can't use Long Text Area
field or Rich Text Area
field in the WHERE
Clause.
When I try to read the field type describe for the field type, both fields return DisplayType.TextArea
, so I can't distinguish them by simply executing the code
static Boolean canBeUsedInWhereClause(DescribeFieldResult fd) {
return fd.getType() != DisplayType.TextArea;
}
since all of these
Opportunity.Test_Text_Area_Long__c.getDescribe().getType();
Opportunity.Test_Text_Area_Rich__c.getDescribe().getType();
Opportunity.Test_Text_Area__c.getDescribe().getType();
return the same value.
I could potentially use additional SOQL query to check if this fails or not
static Boolean canBeUsedInWhereClause(DescribeFieldResult fd) {
try {
List<SObject> records = Database.query('SELECT Name FROM Opportunity WHERE ' + fd.getName() + ' = null');
return true;
} catch (System.QueryException e) {
return false;
}
}
however, this looks ugly and I don't want to run an SOQL
query only to find out if I can display the field in the list of field available for filtering.
Is there any other elegant way to find which field is available to be used in the WHERE
clause? Is there any way to distinguish Text Area
from Long
or Rich Text Area
other than checking if SOQL
query raises an exception?