+ Start a Discussion
gbabeygbabey 

Filtering Knowledge using 'with data category' and strings

I am trying to pass a string representing a data category to a controller and then filter my SOQL query for knowledge based on that.

 

Ideally I would like to do something like:

 

 

    public String release { get; set; }
    
    public List<FAQ__kav> getFAQsForRelease()
    {
        List<FAQ__kav> faqlist = [select id, Title, Answer__c from FAQ__kav WHERE PublishStatus = 'Online' WITH DATA CATEGORY Releases__c ABOVE release];  
        return faqlist;
    }

 

but I can't figure out if there is any possible way to convert the string object 'release' into an datacategory type. Maybe a helper function or something?

 

I realize a big block of if / else if statements would do the trick, but I am hoping there is a cleaner solution. Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Aashay DesaiAashay Desai

Hi again gbabey,

 

I did a little more research and found out that, unfortunately, we don't support bind variables in the WITH DATA CATEGORY clause at this time (sorry, didn't know that earlier).

 

So it looks like you'll have to utilize Dynamic SOQL for now.  If you're populating the string using untrusted values from the web or something, then yes you will have to do some sanitization.  However if you're just passing in string values from a Data Category picklist somewhere, you may not have to worry about that as much (although some basic checks never hurt).

All Answers

Ispita_NavatarIspita_Navatar

Is it imperative that you have to use the operator "ABOVE"  as in case it is not then you may modify your query and use "INCLUDES" which takes string as parameter.

In the meantime if I come across a solution to your precise problem I will keep you posted, in case you get a solution first do share it .

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

gbabeygbabey

Thanks for the quick response Ispita, but it doesn't seem as though INCLUDES is a valid selector for a DataCategorySelection:

 

http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_with_datacategory_filterselectors.htm#sforce_api_calls_soql_select_with_datacategory_filterselectors

 

And just to verify I tried it out and got this error: Compile Error: unexpected token: 'INCLUDES'

Aashay DesaiAashay Desai

Hi there!

 

INCLUDES isn't a valid selector for DataCategorySelection, you're right about that.

 

If I'm reading your code correctly, it looks like you're trying to pass in a string variable into your SOQL query that will ultimately contain the name of your data category.  If that's the case, perhaps this will help:

 

 

  public String release { get; set; }
    
    public List<FAQ__kav> getFAQsForRelease()
    {
        List<FAQ__kav> faqlist = [select id, Title, Answer__c from FAQ__kav WHERE P ublishStatus = 'Online' WITH DATA CATEGORY Releases__c ABOVE :release];  
        return faqlist;
    }

 

Notice the : before the "release" variable.  If you go this route, please do make sure that the string you're passing in is appended with "__c" as Data Categories are considered custom objects.

 

You could optionally use Dynamic SOQL instead, like this:

 

  public String release { get; set; }
    
    public List<FAQ__kav> getFAQsForRelease()
    {
        List<FAQ__kav> faqlist =
Database.query('select id, title, Answer__c from FAQ__kav WHERE PublishStatus=\'Online\' WITH DATA CATEGORY Releases__c ABOVE ' + release + '__c'); 
        return faqlist;
    }

 

In the above example, I assume that the release variable does not contain the __c so I've appended it in the query.  You could always append it to the string variable before you use it in a query too.

 

Let us know how that works out for you!

 

gbabeygbabey

Thanks Aashay, your first solution is exactly what I'm looking for, however I still cannot get it working:

 

     unexpected token: ':' at line 7 column 144

 

The only documentation I could find on the colon syntax was this in the API doc - "Additionally, Apex script variables and expressions can be used if preceded by a colon (:)."  Is this better documented somewhere? Have you gotten this working for Data Categories?

 

I could use your second option, the dynamic SOQL method, but the traditional programmer in me cringes with injection-fear whenever I see strings mixed with SQL

Aashay DesaiAashay Desai

Hi again gbabey,

 

I did a little more research and found out that, unfortunately, we don't support bind variables in the WITH DATA CATEGORY clause at this time (sorry, didn't know that earlier).

 

So it looks like you'll have to utilize Dynamic SOQL for now.  If you're populating the string using untrusted values from the web or something, then yes you will have to do some sanitization.  However if you're just passing in string values from a Data Category picklist somewhere, you may not have to worry about that as much (although some basic checks never hurt).

This was selected as the best answer
gbabeygbabey

Dynamic SOQL it is then. Thanks for all the help!

TankGirlTankGirl

Do you know if they will support this soon?

neuronneuron

Hi gbabey,

 

I am facing the same problem. Even i want to pass a string representing a data category to a controller and then filter my SOQL query for knowledge.

Have you tried it, does it work. Please do reply.

 

Thanks

pintu francis qburstpintu francis qburst

Is it allowed to sort the using ORDER BY in the query? I'm not getting the sorted result.. Always showing unsorted :(