[SalesForce] How to get the Data Category in Select Dynamic Soql

I am trying to fetch Knowledge Articles based on Data Category through the below dynamic Soql query.

String dynamicQuery = 'SELECT Id, Title FROM Knowledge__kav ' +
                                        'WHERE PublishStatus = \'Online\' ' +
                                        'WITH DATA CATEGORY Topics__c AT ({0})';


List<Knowledge__kav> kavList = Database.query(String.format(dynamicQuery, DynamicTopics));

Wherein 'DynamicTopics' is a list of String having the data category name.

The problem i am facing here is, i get all the articles without distinguishing which data category it belongs to.

I cannot put this in for loop as it might hit the limit.

I want the article to be fetched along with their data category or something from which i can distinguishing it with the data category

Can you please help me with this?

Best Answer

You need to use ArticleType_DataCategorySelection object to retrieve ParentId (which is Article Id) and DataCategory Names.

Here is an example from my DE where ArticleType is Documentation_Kav

SELECT Id, ParentId, Parent.Title, DataCategoryGroupName, DataCategoryName 
FROM Documentation__DataCategorySelection
WHERE Parent.PublishStatus = 'Online'

By the way, DataCategoryName cannot be filtered in SOQL query, this is Salesforce Limitation

Like, we cannot execute this query

SELECT Id, ParentId, Parent.Title, DataCategoryGroupName, DataCategoryName 
FROM Documentation__DataCategorySelection
WHERE Parent.PublishStatus = 'Online'
AND DataCategoryName IN ('ABC', 'XYZ')

For more information, refer Article Type__DataCategorySelection

Related Topic