You need to sign in to do that
Don't have an account?
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!
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!
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.
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);
}