Updating published knowledge articles can be tricky, especially when you want to bulk update the published articles.
For example, you want to update the support form URL in all the published articles from an older support form to a new support form.
Old URL: https://mycompany.force.com/support/s/
New URL: https://app.mycompany.com/support/
It has similar steps as you update a single published article. But it requires additional steps to make sure to not publish those draft articles that should stay in draft status. So the steps are
- Create a new draft version of the published article.
- Update the draft version of the article with the changes that you need.
- Update a new temporary checkbox field Article_To_Publish__c with TRUE.
- Finally, publish the article again.
STEP 1
Query the published Articles and create a new version of the article using the editOnlineArticle method from the PublishingService class. This method creates a draft article from the online version and returns the new draft primary version ID of the article. Also, unpublishes the online article, if unpublish is set to true.
list <Knowledge__kav> Articles = [Select Id, ArticleNumber, UrlName, KnowledgeArticleId, Title, PublishStatus, Article_Detail__c FROM Knowledge__kav WHERE PublishStatus = 'Online'];
for(Knowledge__kav article : Articles){
if(article.Article_Detail__c.ContainsIgnoreCase('https://mycompany.force.com/support/s/')){
KbManagement.PublishingService.editOnlineArticle (article.KnowledgeArticleId, false);
system.debug(article.Id);
}
}
STEP 2
Query the draft articles and update the URL in the Article_Detail__c field. You can update whatever you want. Then also set the new checkbox field Article_To_Publish__c to TRUE.
list <Knowledge__kav> Articles = [Select Id,KnowledgeArticleId, Title, PublishStatus, Article_Detail__c, Article_To_Publish__c FROM Knowledge__kav WHERE PublishStatus = 'Draft'];
list <Knowledge__kav> ArticlesToUpdate = new list <Knowledge__kav> ();
for(Knowledge__kav article : Articles){
if(article.Article_Detail__c.ContainsIgnoreCase('https://mycompany.force.com/support/s/')){
article.Article_Detail__c = article.Article_Detail__c.replaceAll('https://mycompany.force.com/support/s/', 'https://app.mycompany.com/support/');
article.Article_To_Publish__c = true;
ArticlesToUpdate.add(article);
system.debug(article.Id);
}
}
update ArticlesToUpdate;
STEP 3
Now publish the article again using the publishArticle method from the PublishingService class. This method publishes an article. If flagAsNew is set to true, the article is published as a major version.
list <Knowledge__kav> Articles = [Select Id,KnowledgeArticleId, Title, PublishStatus, Article_Detail__c, Article_To_Publish__c FROM Knowledge__kav WHERE PublishStatus = 'Draft' AND Article_To_Publish__c = true];
for(Knowledge__kav article : Articles){
KbManagement.PublishingService.publishArticle(article.KnowledgeArticleId, false);
system.debug(article.Id);
}