PROBLEM STATEMENT
- Enabling Lightning Knowledge changes your Org’s Data Model to use Record Types rather than Article Types. Orgs with multiple articles types require data migration to consolidate article types before enabling Lightning Knowledge because Record type field will be blank on all existing Articles.
- We migrate knowledge from other platform to Salesforce in bulk, but unfortunately can not publish in bulk.
- Sometimes, we get requirement to update something on knowledge in bulk, but we have to draft it first, update it and then publish it. A lot of work.
SOLUTION
Below given solution is use to Apex script to query Knowledge and make change and then publish in mass. This peice of code further can be used anywhere in Anonymous Window (Developer Console –> Debug –> Open Execute Anonymous window) or Apex Class/Trigger.
Query Published articles and make desired changes
(skip if you don’t want to make any update and just want to publish draft articles, goto next step)
//First make published articles Draft: List arts = [SELECT Id, KnowledgeArticleId, IsVisibleInPrm FROM Article__kav WHERE PublishStatus ='Online' AND Language='en_US' AND RecordTypeId= null]; for(Article__kav art: arts){ String articleId = art.KnowledgeArticleId; //keep published version online and create a draft with help of 'false' parameter try{ String id = KbManagement.PublishingService.editOnlineArticle (articleId, false); }catch(exception e){ //if draft exist already, an error will be thrown. handle it. } } //UPDATE THE ARTICLE String recordtypeid = '0120x0000008ZTE'; //replace your record type...and Article object api name List arts = [SELECT Id, KnowledgeArticleId, RecordTypeId FROM Article__kav WHERE PublishStatus ='Draft' AND Language='en_US' AND RecordTypeId= null]; for(Article__kav art: arts){ system.debug(art.Id); art.RecordTypeId= recordtypeid; } update arts;
Publish the Articles in Bulk
Run below code to publish the articles. Do in batch of 100 and adjust if needed.
for(Knowledge__kav art: [select KnowledgeArticleId from knowledge__kav where publishStatus='Draft' limit 100]){ String articleId = art.KnowledgeArticleId; //false parameter do not create a new version of article. it's imp to understand. KbManagement.PublishingService.publishArticle(articleId, false); }
OTHER NOTES
- Read parameters
KbManagement.PublishingService
carefully otherwise you may end up a lot of drafted versions or move online article to draft completely. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_knowledge_kbManagement.htm - You may face known issues of salesforce while updating this: https://success.salesforce.com/issues_view?id=a1p3A0000017wpcQAA
- If there are huge set of records then use LIMIT in Queries.
- A similar discussion: https://salesforce.stackexchange.com/questions/115592/mass-updating-published-articles
- Current User must have:
Knowledge User
Checked on your user record detail page.- “Manage Articles” permission is assigned
- “Read”, “Create”, and “Edit” on the article type