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
paul-lmipaul-lmi 

Using Apex to get DataCategorySelection from a specific Knowledge Article

I'm wondering if this is possible.  We're basically building a bigger, better, more scalable editor interface, pretty much replacing components that don't scale (list views, filters) with our own VF, and one of the requests I got was to grab the data categories associated with the article when listing them.

 

Should something like this work?

 

Select f.Localized_Title__c, f.Id, (Select ParentId, DataCategoryGroupName, DataCategoryName From DataCategorySelections) From FAQ__kav f where PublishStatus = 'Online'

 

I know I'd have to iterate through the data categories in separate logic before binding to VF (or do it in an apex:repeat), but theoretically, no issues here right?

Best Answer chosen by Admin (Salesforce Developers) 
etienne.giraudyetienne.giraudy

Paul,

Yes here you are facing a limit of SOSL: it doesn't support nested SELECT statements.

Thanks,

Etienne

All Answers

paul-lmipaul-lmi

So this is possible.  Ran into an issue with the query though.  Is it illegal to mix SOSL with SOQL subqueries?

 

broken up for readability

 

FIND 'access' IN ALL FIELDS

RETURNING

FAQ__kav(ID, knowledgearticleid, Title, Localized_Title__c, UrlName, LastPublishedDate, (SELECT ParentId, DataCategoryGroupName, DataCategoryName From DataCategorySelections)

WHERE PublishStatus = 'Online' LIMIT 20)

WITH DATA CATEGORY Products__c AT (lmipro2__c) AND Languages__c AT (en__c)

 

If I do this as a vanilla SELECT query, it works, but I'm looking for performance.  Our KB has thousands of docs in it already.  This also works in SOSL prior to adding the subquery for the DataCategorySelections.

 

Error is

17:37:57.289|EXCEPTION_THROWN|[223]|System.QueryException: unexpected token: '('

 

I am not sure if this is a syntax error, or a "Salesforce doesn't support this type of query" error.  Pushing some limits here.

etienne.giraudyetienne.giraudy

Paul,

Yes here you are facing a limit of SOSL: it doesn't support nested SELECT statements.

Thanks,

Etienne

This was selected as the best answer
paul-lmipaul-lmi

thanks Etienne.  i'll have to work around that in some other creative way.

TankGirlTankGirl

I found a way and the key is this, if you want to access the datacategory's of that Artecal type you need to use Artical_type__DataCategorySelection for example if we wanted to get the DataCategorys for a FAQ__kav you would use 'FAQ__DataCategorySelection'

 

Here is an example query:

 

//Do a parent  to child Query

FAQ__kav[] faq = [Select id,(Select Id, ParentId, DataCategoryGroupName, DataCategoryName, CreatedDate, CreatedById, IsDeleted, SystemModstamp From DataCategorySelections) From FAQ__kav where (id=:knowledgeId or KnowledgeArticleId=:knowledgeId) and PublishStatus = 'Online' limit 200];

 

//Check if the child is not null 

if (faq[0].DataCategorySelections.size() > 0){

 

// for loop to access the child

for(FAQ__kav f : faq) {

FAQ__DataCategorySelection[] dc =  f.DataCategorySelections;

// for loop to access each child returned in the query

for(FAQ__DataCategorySelection d : dc){

 

// do something with the data

if(d.DataCategoryGroupName == 'products'){

dcList.add(d.DataCategoryName);               

}

}


}       

 

}