21

I'm working on a custom search page, which will search through the fields of the Project object. I use dynamic SOQL query similar to that:

String query = 'SELECT Name, Status__c ... 
    FROM Project__c WHERE Name LIKE \'%' + String.escapeSingleQuotes(name)
     +'%\' + ' OR Status__c = \'' + String.escapeSingleQuotes(status) +'\'';
List<Project__c> projects = Database.query(query);

But when I try to add to filter criteria Description__c field, which is Long Text Area, get the following error:

System.QueryException: field 'Description__c' can not be filtered in query call

How can I use Long Text Area fields in filtering of results?

1
  • 1
    What values are in that field that you want to filter on ? Possible it shouldn't be a long text field, or you could optimize your data model and process by creating additional fields. Oct 29, 2014 at 12:01

5 Answers 5

36

You can not use text area fields in SOQL and SOSL filter criterias.

Try this approach:

List<Project__c> projects = new List<Project__c>();
for(Project__c proj : [SELECT Name, Status__c, Description__c FROM Project__c WHERE Name LIKE '%' + String.escapeSingleQuotes(name) +'%' + OR Status__c = String.escapeSingleQuotes(status)]) {
    if(proj.Description__c.contains('filter string')) {
        projects.add(proj);
    }
}
12

There is an Idea to Allow filtering on Long Text Area, so you can't. In a pinch, you could iterate over your results in a for loop and use String methods to filter out/in the desired results. Of course, that may not be reasonable depending on the number of records and whether or not what you are doing is needed in real-time.

3

I just discovered a work around for this here. Create a Formula field that uses the LEFT formula and filter on that. You cannot filter on the ENTIRE long text but you can filter on part of it:

Legal_Street__c is a long text area. I created a formula field called StreetConc__c:

LEFT(Legal_Street__c, 40)

I then created a dummy record and put the text 'testingthis' in the Legal_Street__c field. From here I filtered on this formula field.

System.debug('answer = ' + [SELECT Name FROM Account WHERE StreetConc__c LIKE 'testing%']);

You should be able to apply this to both SOQL and SQL.

5
  • 4
    Can you still get this to work? It does not work for me, I'm getting a message telling me that I cannot use fields of type Long Text Area in a formula. Mar 22, 2016 at 6:51
  • I dont think this is possible, are you sure your field Legal_Street__c is long text and not text area?
    – manza
    Jul 1, 2017 at 5:53
  • 2
    An variant of this is to create a formula that 'implements the where clause' (or part of it). E.g if you have WHERE longtextarea__c like '%TEST%', you can create a formula field x__c with formula CONTAINS(longtextarea__c, "TEST") and make the query WHERE x__c = TRUE .
    – Legolas
    Dec 7, 2017 at 13:25
  • CONTAINS(longtextarea__c, "TEST") gives error You referenced an unsupported field type called "Long Text Area" using the following field: now
    – twamley
    May 29, 2020 at 21:25
  • 3
    LONGTEXT are NOT allowed in a formula field anymore! Nov 3, 2020 at 21:44
3

The other options here are good for using logic during page render or during data processing. But if you have a lot happening at this stage of your process these could be heavy. (maybe maybe not).

Another option which will alow you to filter on your situation and reduce the number of records returned.

Use a BeforeTrigger when saving each record to set a boolean or smaller text field. Then query/filter on that new boolean or text.

TRIGGER:

trigger MyObjectBeforeInsertUpdate on Booking__c (before insert, before update) {
    for(myObject__c item:objectList){
        item.hasBoolean__c = !string.ISBLANK(item.largeTextField__c);
    }
}//code not tested.
0

This is an old thread but I hope my SOSL solution can help others. Actually, we can use SOSL as a workaround to turn Textarea field types into filterable/searchable field. See code sample below

String fieldValue = 'somevalue';
String fieldName = 'MyTextAreaField__c';
String query =  = 'FIND {"'+ fieldValue + '"}  RETURNING ' +
                  ' YourCustomObject__c (id, ' + fieldName + ' ) LIMIT 2000';
List<List<SObject>> searchList = Search.query(query);//Return List of SObject list
system.debug('Search result: ' + searchList[0]);//Get search result
if(searchList[0].size() > 0) {
   //You can cast your result and return other fields if needed
   List<YourCustomObject__c > objSearchResultsList = (List<YourCustomObject__c >) searchList[0];
   system.debug(fieldName +' with "' + objSearchResultsList[0].get(fieldName) + '" found. ');
 }

If your search result has record, the sample output will be:

Search result: (YourCustomObject__c:{Id=aeb02000000000LXXX, MyTextAreaField__c=somevalue})   
MyTextAreaField__c with "somevalue" found. 

You must log in to answer this question.

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