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: 000001000Knowledge Article Id: kA03t00000063w2CAAFetching Latest Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND IsLatestVersion = TrueFetching Published Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘Online’Fetching Draft Knowledge Article VersionSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘Draft’Fetching Archived Knowledge Article VersionsSELECT Id,PublishStatus,Title FROM Knowledge__kav WHERE KnowledgeArticleId = ‘kA03t00000063w2CAA’ AND PublishStatus = ‘archived’ AND IsLatestVersion = FalseThis 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 CATEGORYWITH DATA CATEGORY is an optional clause in SOQL and it helps to identify articles linked with one or more...

Read More