How to mass update Knowledge Article in Salesforce?

on

|

views

and

comments

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: –

  1. As this is a time taking process it will not be able to process all the articles at once
  2. I have tested this for 75 records at once and it was working like charm

Thanks for reading

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

2 COMMENTS

  1. 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential