Hello #Trailblazers,
Welcome back, in this video we will talk about how to mass update articles in Salesforce using Apex.
Problem
The problem is sometimes the salesforce admin publish the Salesforce Knowledge Articles without making it available to the customers or guest users for Salesforce Community.
Now, if admin wants to check “Visible in Public Knowledge Base” checkbox for multiple articles first you need to edit all the articles one by one which will be headache and time consuming.
I have come with the work around which you can use to update the articles in bulk.
Update a single Knowledge articles
List<Knowledge__kav> knowArticleRec = [select id, IsVisibleInPkb, KnowledgeArticleId
from Knowledge__kav where PublishStatus = 'Online' and Id= 'ka0P0000000EUzHIAW'];
Id kaId = knowArticleRec[0].KnowledgeArticleId;
// Create a new version of knowledge article and do not unpublish.
KbManagement.PublishingService.editOnlineArticle(knowArticleRec[0].KnowledgeArticleId, false);
knowArticleRec = [SELECT Id, KnowledgeArticleId, PublishStatus, IsVisibleInPkb FROM Knowledge__kav WHERE KnowledgeArticleId = : kaId and PublishStatus = 'Draft' limit 1];
IsVisibleInApp (internal app), and IsVisibleInPkb (Public KnowledgeBase)
Knowledge__kav newKAV = new Knowledge__kav(id = knowArticleRec[0].Id, IsVisibleInPkb = true);
update newKav;
KbManagement.PublishingService.publishArticle(kaId, false);
Update bulk Knowledge articles
Map<Id,Knowledge__kav> kavsToProcess = new Map<Id,Knowledge__kav>([select Id, Title,IsVisibleInPkb, knowledgearticleid from Knowledge__kav where IsLatestVersion = true and IsVisibleInPkb = false And PublishStatus = 'Online' limit 76]);
Set<Id> knowledgeArticleIds = new Set<Id>();
for (Knowledge__kav thisKAV : kavsToProcess.values()){
knowledgeArticleIds.add(thisKAV.KnowledgeArticleId);
}
List<Knowledge__kav> draftKAVs = [select id,IsVisibleInPkb,PublishStatus, KnowledgeArticleId,Title from Knowledge__kav where PublishStatus = 'Draft' and KnowledgeArticleId in : knowledgeArticleIds];
List<Knowledge__kav> toUpdate = new List<Knowledge__kav>();
Set<Id> konwledgeArticlesWithDrafts = new Set<Id>();
for (Knowledge__kav draftKAV : draftKAVs){
konwledgeArticlesWithDrafts.add(draftKav.KnowledgeArticleId);
}
for (Knowledge__kav thisKAV : kavsToProcess.values()){
if (!konwledgeArticlesWithDrafts.contains(thisKav.KnowledgeArticleId)) {
KbManagement.PublishingService.editOnlineArticle(thisKAV.KnowledgeArticleId, false);
}
}
kavsToProcess = new Map<Id,Knowledge__kav>([SELECT Id, KnowledgeArticleId, IsVisibleInPkb, PublishStatus FROM Knowledge__kav WHERE KnowledgeArticleId IN : knowledgeArticleIds and PublishStatus = 'Draft' and (NOT(Id IN : kasWithDrafts))]);
for (Knowledge__kav thisKAV : kavsToProcess.values()) {
toUpdate.add(new Knowledge__kav (Id = thisKAV.Id, IsVisibleInPkb = true));
}
update toUpdate;
for (Knowledge__kav thisKAV : kavsToProcess.values()){
if (!kasWithDrafts.contains(thisKav.KnowledgeArticleId)) {
KbManagement.PublishingService.publishArticle(thisKAV.KnowledgeArticleId, false);
}
}
Note: –
- As this is a time taking process it will not be able to process all the articles at once
- I have tested this for 75 records at once and it was working like charm
Thanks for reading
I would really like to get this to work. I know very little about APEX. I tried to execute this code in Open Execute Anonymous Window and am getting an error that says Variable does not exist: kaIds.
Any suggestions? I simply need to update Visible in Public Knowledge Base (IsVisibleInPkb) and re-Publish about 500 articles.
I will greatly appreciate any help!
Try to use knowledgeArticleIds instead of kaIds and it should work. Also, I will suggest first try with 1 Article and see if this updates is correctly and then you can go with bulk records.