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 revision without unpublishing.  KbManagement.PublishingService.editOnlineArticle(knowArticleRec[0].KnowledgeArticleId, false);    // Requery to get the new version ID.  knowArticleRec = [SELECT Id, KnowledgeArticleId, PublishStatus, IsVisibleInPkb FROM Knowledge__kav WHERE KnowledgeArticleId = : kaId and PublishStatus = 'Draft' limit 1];    // Set to be visible to Customers. There is also IsVisibleInPrm (Partner Community), IsVisibleInApp (internal app), and IsVisibleInPkb (Public KnowledgeBase)  Knowledge__kav newKAV = new Knowledge__kav(id = knowArticleRec[0].Id, IsVisibleInPkb = true);  update newKav;    // Republish without a new version  KbManagement.PublishingService.publishArticle(kaId, false);

Update bulk Knowledge articles

// Query for the online versions   // Since this is heavy, process 10 at a time.  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]);    // Run over the KA Versions and collect the KnowledgeArticle ID's.       Set<Id> kaIds = new Set<Id>();  for (Knowledge__kav thisKAV : kavsToProcess.values()){      kaIds.add(thisKAV.KnowledgeArticleId);  }    // Query for EXISTING draft versions of our published articles. We'll exclude them from our updates.  List<Knowledge__kav> draftKAVs = [select id,IsVisibleInPkb,PublishStatus, KnowledgeArticleId,Title from Knowledge__kav where PublishStatus = 'Draft' and KnowledgeArticleId in : kaIds];    List<Knowledge__kav> toUpdate = new List<Knowledge__kav>();    // Go over the KnowledgeArticleVersions from the 2nd query and create a list (Set) of KnowledgeArticle ID's that  // have an existing draft.  Set<Id> kasWithDrafts = new Set<Id>();  for (Knowledge__kav draftKAV : draftKAVs){      kasWithDrafts.add(draftKav.KnowledgeArticleId);      System.debug('Eliminating Article From Processing, Already Has Draft: ' + draftKav.Title + ' ' + draftKav.id);      /* If this version doesn't have our flag, add it to the list to update.      if (draftKav.IsVisibleInPkb == false){          toUpdate.add(new Knowledge__kav(Id = draftKAV.Id, IsVisibleInPkb = true));      }  	*/  }    // Now, go back over the original list of published versions and create an un-versioned revision of any Article that does not   // have a pre-existing draft.  for (Knowledge__kav thisKAV : kavsToProcess.values()){      if (!kasWithDrafts.contains(thisKav.KnowledgeArticleId)) {          System.debug('Updating ' + thisKav.Title);          KbManagement.PublishingService.editOnlineArticle(thisKAV.KnowledgeArticleId, false);      }  }    System.debug('kasWithDrafts ==' + kasWithDrafts.size());    // Requery to get all the Knowledge Article VERSION Id's created above from editOnlineArticle().  // Note that this EXCLUDES the KnowledgeArticles that already have a draft.  kavsToProcess = new Map<Id,Knowledge__kav>([SELECT Id, KnowledgeArticleId, IsVisibleInPkb, PublishStatus FROM Knowledge__kav WHERE KnowledgeArticleId IN : kaIds and PublishStatus = 'Draft' and (NOT(Id IN : kasWithDrafts))]);    System.debug('Size of kavsToProcess' + kavsToProcess.size());    // Go over all our newly created versions, and update them with the attribute we're going for (IsVisibleInPkb)  for (Knowledge__kav thisKAV : kavsToProcess.values()) {      toUpdate.add(new Knowledge__kav (Id = thisKAV.Id, IsVisibleInPkb = true));  }  update toUpdate;    // Now publish the new versions that we have updated.  kavsToProcess  for (Knowledge__kav thisKAV : kavsToProcess.values()){  	  	System.debug('KnowledgeArticleId Id' + thisKAV.KnowledgeArticleId);  	System.debug('PublishStatus' + thisKAV.PublishStatus);  	if (!kasWithDrafts.contains(thisKav.KnowledgeArticleId)) {  			KbManagement.PublishingService.publishArticle(thisKAV.KnowledgeArticleId, false);  	}  }    System.debug('Total Updated: ' + toUpdate.size());    System.debug('Success');

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