Update Knowledge Articles in Bulk
Recently we had a big Knowledge Centric Support (KCS) initiative where we generated a ton of Articles for use in a Community. (We're still on Classic Knowledge.)
Problem is, some users forgot to tick the "Customer" box when creating the Articles. So those articles didn't appear to external users in the Community.
You can update these Articles in bulk, but you need to use a few obscure hooks in Apex. This method will create a draft of the Article, update the Knowledge Article Version, and then re-publish without creating a new Version.
The following code will update the 'IsVisibleInCsp' flag for a single Article specified in the 1st query, which will make it visible to Customers.
Here's a bulkified version. This is heavy on the CPU so I run it 10 at a time.
When going over these in bulk, you might run into a situation where there is an existing draft of an article that has been previously published; i.e., it's actively being updated. In this example, we search for those situations and don't create revisions of them.
This will go over all articles of type BA_Public_Knowledgebase__kav, 10 at a time. Update the code for your KnowledgeArticleVersion name, then run in Execute Anonymous. When you're done it will report that it updated 0 articles.
Problem is, some users forgot to tick the "Customer" box when creating the Articles. So those articles didn't appear to external users in the Community.
You can update these Articles in bulk, but you need to use a few obscure hooks in Apex. This method will create a draft of the Article, update the Knowledge Article Version, and then re-publish without creating a new Version.
The following code will update the 'IsVisibleInCsp' flag for a single Article specified in the 1st query, which will make it visible to Customers.
Here's a bulkified version. This is heavy on the CPU so I run it 10 at a time.
When going over these in bulk, you might run into a situation where there is an existing draft of an article that has been previously published; i.e., it's actively being updated. In this example, we search for those situations and don't create revisions of them.
This will go over all articles of type BA_Public_Knowledgebase__kav, 10 at a time. Update the code for your KnowledgeArticleVersion name, then run in Execute Anonymous. When you're done it will report that it updated 0 articles.
This is awesome. It helped me do the same for one of our requirements. I am facing one challenge though and unable to figure it out. It worked seamlessly for one article type but not working for any other article types. I am a system admin user and have all access to publish articles. Not sure what is the reason. Keep getting the below error:
ReplyDeleteSystem.HandledException: You can't perform this action. Be sure the action is valid for the current state of the article, and that you have permission to perform it.
hi, I have a question for this method KbManagement.PublishingService.editOnlineArticle, I tried to replace the current online article version with a new one, but it seems that the editOnlineArticle with param unpublish set to TRUE do not works as expected creating a new version record (__KAV) with status Draft, on the contrary I got an unexpected behaviour, the method only update the current online version record to a new version number and changed the publish status field to Draft. Should this be the right logic? I went through the standard interface to create a new article version and from this action I got a new __KAV record with version 0 and publish status as Draft, having at the end two records (__KAV) for the same article, on for Draft and another for Online. I am just wandering if this is the default logic when creating versions from apex. thxs!
ReplyDelete