function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bryan TelfordBryan Telford 

Querying with LIKE against a list of values

I have the following code.

 webService static List<Attachment> getSearchedFiles(String query) {
       String[] searchKeys = query.split(' ');

    for (Integer i = 0; i < searchKeys.size(); i++) {
        searchKeys[i] = '%' + searchKeys[i] + '%';
    }
      system.debug(searchKeys);    
       List<Document_Folder__c> files = [
           select 
            id, 
            (
               select id, Name, Description, BodyLength, CreatedDate, CreatedBy.Name, LastModifiedDate, LastModifiedBy.Name, ParentId,         Parent.Name 
               from attachments 
               where Name LIKE :searchKeys order by Name
            ), 
           from Document_Folder__c
       ];

The debug log shows the correct value that I want.
11:51:47.0 (31583876)|USER_DEBUG|[40]|DEBUG|(%chat%, %talk%)

But no query results are returned. Any thoughts? 

Thanks!
 
James LoghryJames Loghry

Hi Bryan,

The Like query looks syntactically correct.  I just tried a similar query in my dev org, and it works as expected.

You're using a subquery on Document_Folder__c though, so I'm curious if you're not getting any Document_Folder__c records back?

A few simple queries I would try first to see where the exact problem is:

1) Select Id From Document_Folder__c to see if any records are returned.
2) Select Id From Attachment Where Name Like :searchKeys (Removing any of the Joins)
3) Select Id From Attachment Where ParentId = <some document folder id>

That should help you narrow down where the problem is.

Bryan TelfordBryan Telford
Hi James, 

Thanks for the suggestions. I tried all 3 and they return records. Also, I can execute the following the apex anonymous window and the "searchKeys" list returns. The difference seems to be that one is a sub-query and the other is not. Strange.

String searchTerm = 'chat talk';
String[] searchKeys = searchTerm.split(' ');
 
for (Integer i = 0; i < searchKeys.size(); i++) {
    searchKeys[i] = '%' + searchKeys[i] + '%';
}
 
List<Attachment> attachments = [
select id, Name, Description
from attachment 
where Name LIKE :searchKeys order by Name
];
 
for (Attachment a : attachments) {
    System.debug(a.name);
}
 
James LoghryJames Loghry
Could it be a data visibilty issue?  If you change the apex class to use "without sharing" instead of "with sharing", then do the records come back?  If so, you'll want to check that the OWD and sharing settings are set up properly for the Document_Folder__c object.
Bryan TelfordBryan Telford
It turns out the Apex was fine. The issue was with the way the VF was passing in the searchKeys parameter to the class. All fixed. Thanks for your replies!