In continuation of my effort to make Lightning Knowledge easily understandable for all, today I am going to show how Salesforce Knowledge can be accessed using Apex. Salesforce Knowledge can have Apex Triggers and be accessible through Apex code. Actually, Salesforce provides some standard apex classes related to Knowledge Management that can be called for actions like publishing, editing as draft, archiving, and more.
Before I start, I would recommend you going through my earlier posts –
We can use the methods in KbManagement.PublishingService class to manage the lifecycle of an article. I am going to explain some of the few classes which developer needs mostly, but to know about all the methods, I highly recommend you going through the documentation here.
Editing an Article as Draft
We can use the below method to edit an article as draft –
Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).
2nd parameter is a Boolean value where false will not unpublish the already published articles and true will unpublish the already published article.
This method will return the new draft master version ID of the article.
Note – We need to keep this in mind that this method is not bulkified. So if you are trying to bulk edit a lot of published articles, this method will fail as it will throw Salesforce Limit. So the workaround is to implement Queueable class like below, where you call the queueable method with the batch size of your choice.
Hi! Can we create a custom vf page to display queried knowledge articles? Thanks in advance.
Absolutely you can. But there are better way to display knowledge articles through flow (declarative way). In my next post, I am going to show how to do Knowledge operations in Flow.
Also there is a post few weeks back where I showed how you can use the Salesforce Lab DataGrid solution to display records in flow. You can have a look at – https://www.sudipta-deb.in/2020/08/show-your-data-in-datatable-map-or-tile.html
I also have the use case of conditionally displaying a specific knowledge article based upon a toggle being true if a Screen flow. Is this possible?