Ticker

6/recent/ticker-posts

Header Ads Widget

Handy SOQL/SOSL Queries for Knowledge

 


Happy Monday!! In my last post, I have explained the Data Model around Salesforce Lightning Knowledge. If you haven't gone through that post, I highly recommend you going through the post "Understanding Salesforce Lightning Knowledge Data Model" as it is going to help you to understand the queries in this post.

Let's first create the article - "Hello World" and below are the different versions with different publishing status. Below article properties which I will be using the query -

  • Article Number: 000001000
  • Knowledge Article Id: kA03t00000063w2CAA













Fetching Latest Knowledge Article Version

SELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = 'kA03t00000063w2CAA' AND IsLatestVersion = True


Fetching Published Knowledge Article Version

SELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = 'kA03t00000063w2CAA' AND PublishStatus = 'Online'


Fetching Draft Knowledge Article Version

SELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = 'kA03t00000063w2CAA' AND PublishStatus = 'Draft'


Fetching Archived Knowledge Article Versions

SELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = 'kA03t00000063w2CAA' AND PublishStatus = 'archived' AND IsLatestVersion = False

This one is tricky, as to query Archived article versions, KnowledgeArticleId and IsLatestVersion = False and PublishStatus = archived should be mentioned.

Note -
Using bind variables with Apex SOQL Statements with KnowledgeArticleVersion is not allowed. So you need to use dynamic SOQL like below -
final String ONLINE_ARTICLE = 'Online';
final String myQuery = 'SELECT Id FROM Knowledge__kav WHERE PublishStatus = :ONLINE_ARTICLE';
List<Knowledge__kav> allArtciles = Database.query(myQuery);

Working with DATA CATEGORY

WITH DATA CATEGORY is an optional clause in SOQL and it helps to identify articles linked with one or more data categories.
You can use WITH DATA CATEGORY to fetch articles FROM ObjectTypeName where 
  • ObjectTypeName = KnowledgeArticleVersion to query all articles types.
  • ObjectTypeName = an article type API Name to query a specific article type.
  • ObjectTypeName = Question to query questions.
Note - When ObjectTypeName = KnowledgeArticleVersion or any article type API Name, then WHERE clause is mandatory. You can put one of the below things in the WHERE clause -
  • PublishStatus 
    • "online" to published articles.
    • "archived" to query archived articles
    • "draft" to query draft articles
  • Id = articles Id.
SOQL Query -
SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY NorthAmerica__c ABOVE canada__c

Note - WITH DATA CATEGORY, only AND logical operator is allowed. 


DATA CATEGORY Filtering Selectors

Let's say we have the below data category structure -
Earth__c
    Asia__c
        India__c
        Bangladesh__c
    NorthAmerica__c
        Canada__c
        USA__c
    Europe__c
        Switzerland__c
        France__c
        German__c

Example 1: Fetch all published articles from Canada 
SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY Earth__c AT Canada__c

Example 2: Fetch all published articles above German
SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY Earth__c ABOVE German__c
This will fetch articles linked with German, Europe, Earth data categories.

Example 3: Fetch all published articles below Asia
SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY Earth__c BELOW Asia__c
This will fetch articles linked with Asia, India, Bangladesh data categories.

Example 3: Fetch all published articles above or below Europe
SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY Earth__c ABOVE_OR_BELOW Europe__c
This will fetch articles linked with Europe, Switzerland, France, German, Earth data categories.

To know more about, please read this great section here.

Thank you and please let me know if you have any handy queries in the comment section.


Post a Comment

0 Comments