I need to search for a list of strings and match against a picklist field. So far, the query below only works when there is only one string in the query string. Appending more strings does not work since I think it is matching against the entire string.
for example
Arts and Culture, Animal Welfare
does not return any results. But if you pass either of them, there is a result.
issueAreas = [SELECT Name, Description__c FROM Engagements__c WHERE ( Other_Issue_Area__c INCLUDES ( :searchString ) OR Primary_Issue_Area__c IN (:searchString)) ];
Entire code
both Other_Issue_Area__c and Primary_Issue_Area__c are picklist fields
public with sharing class GetSearchResults {
@AuraEnabled(cacheable=true)
public static List<Engagements__c> getSearchResults(Object searchTexts) {
String message = 'Success';
String searchString;
String convStr;
String searchQuery;
Set<String> searchSet = new Set<String>();
List<String> searchArray = new List<String>();
List<String> searchList = new List<String>();
List<String> finalSearch = new List<String>();
List<Engagements__c> issueAreas = new List<Engagements__c>();
//List<List<SObject>> results = new List<List<SObject>>();
List<String> res = new List<String>();
try{
if(searchTexts != null){
System.debug('Received ' + searchTexts);
convStr = String.valueOf(searchTexts);
String processedString = convStr.remove('(');
convStr = processedString.remove(')');
System.debug('Processed String ' + convStr);
searchList = convStr.split(',');
searchSet.addAll(searchList);
searchSet.addAll(searchSet); //Removes the remaining duplicate value;
searchArray.addAll(searchSet);
for(String s : searchArray){
// s = '"'+ s + '"';
s = '\''+ s + '\'';
//s = '*' + s + '*';
finalSearch.add(s);
searchSet.add(s);
}
System.debug('Modified list ' + searchArray);
}
searchString = String.join(searchArray, ',');
//searchString = '(' + searchString + ')';
System.debug('Search conv string ' + searchSet);
//issueAreas = [SELECT Name, Description__c FROM Engagements__c WHERE ( Other_Issue_Area__c INCLUDES ( :searchString ) OR Primary_Issue_Area__c IN (:searchString)) ];
issueAreas = [SELECT Name, Description__c FROM Engagements__c WHERE ( Other_Issue_Area__c INCLUDES ( :finalSearch ) OR Primary_Issue_Area__c IN (:finalSearch)) ];
// for(String s : searchSet){
// issueAreas = [SELECT Id FROM Engagements__c WHERE Other_Issue_Area__c INCLUDES (:s) ];
// for(Engagements__c e: issueAreas){
// String issueId = e.Id;
// res.add(issueId);
// }
// }
System.debug('Number of issues ' + issueAreas.size());
}
catch(Exception e){
System.debug('Error occured' + e.getMessage());
}
return issueAreas;
}
}
I tried the line below passing a list to INCLUDES which is what I found works for picklist fields but INCLUDES does not take a list.
issueAreas = [SELECT Name, Description__c FROM Engagements__c WHERE ( Other_Issue_Area__c INCLUDES ( :finalSearch ) OR Primary_Issue_Area__c IN (:finalSearch)) ];
So I thought I would have to use a loop(commented) and then check limits but I am trying to avoid that approach. I can't find other alternatives.
Any help will be appreciated.
Thanks.