2

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?

2 Answers 2

3

There is method in the DescribeFieldResult to determine if the field can be used as part of the filter criteria of a WHERE statement, this method isFilterable() can be used to distinguish the field type

System.debug('@@@ field LONG ' + Opportunity.Test_Text_Area_Long__c.getDescribe().isFilterable() ); // False
System.debug('@@@ field Rich ' + Opportunity.Test_Text_Area_Rich__c.getDescribe().isFilterable()); // False
System.debug('@@@ field Text ' + Opportunity.Test_Text_Area__c.getDescribe().isFilterable()); // True

And the method to determine if field can be used in the WHERE clause may just call this method isFilterable

static Boolean canBeUsedInWhereClause(DescribeFieldResult fd) {
   return fd.isFilterable();
}

Update:

Found some additional way of distinction, it is possible to query the ExtraTypeInfo field on FieldDefinition:

SELECT DeveloperName, ExtraTypeInfo FROM FieldDefinition WHERE EntityDefinitionId = 'Opportunity' AND DeveloperName LIKE 'Test_Text_Area%'

which can help to distinguish between Rich and Long Text Area fields

Test_Text_Area_Long plaintextarea   
Test_Text_Area_Rich richtextarea    
Test_Text_Area  plaintextarea
3
  • 1
    That's pretty clever, though I wish there was a more explicit property that describes RTAs vs plain text fields.
    – sfdcfox
    Commented Sep 22, 2021 at 12:59
  • Agree. My initial thought was to check for isSortable() and this is how I implemented in the real project, but when I was preparing this post with answer I have double checked the documentation and figured out there is actually isFilterable() method which looks more appropriate for my case
    – Patlatus
    Commented Sep 22, 2021 at 13:00
  • @sfdcfox there is actually a property ExtraTypeInfo on FieldDefinition that describes RTAs vs plain text fields. however, it is not available through Field Describe
    – Patlatus
    Commented Sep 24, 2021 at 8:15
0

You can use the method isHtmlFormatted to distinguish between the two

isHtmlFormatted()

Returns true if the field has been formatted for HTML and should be encoded for display in HTML, false otherwise. One example of a field that returns true for this method is a hyperlink custom formula field. Another example is a custom formula field that has an IMAGE text function.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_fields_describe.htm#apex_Schema_DescribeFieldResult_getType

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .