How to Eliminate the Bulky Manual Work For Salesforce Knowledge

December 4, 2015 Appirio

By Lalit Malav

techbl

Getting useful Salesforce knowledge to service agents is an important part of a successful Service Cloud implementation. Unfortunately, moving knowledge into Salesforce has often meant bulky manual work. In this article, we will learn to reduce the developer efforts and automate the following scenarios/problems for Salesforce knowledge articles:

  1. Submitting translations for all published articles in a particular language.
  2. Updating a field value of all published articles.

Batch utility to submit translations for all published support articles

The first scenario is a very common occurrence. For example, we have 20,000 support articles in our org to submit for translation to Spanish_Mexico. It is inconvenient to submit the translation manually for Spanish one by one. In order to reduce the developer’s manual efforts, you can use the below batch utility class to submit the translations for all of your support articles:

[snippet]

global with sharing class SubmitTranslationInSpanishBatch implements Database.Batchable <sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC) {

    return Database.getQueryLocator([Select id, KnowledgeArticleId from FAQ__kav where PublishStatus=’Online’ AND language =’en_US’]);

}

global void execute(Database.BatchableContext BC, List<sObject> supportArticles){

   for(FAQ__kav article : (List<FAQ__kav>)supportArticles){

   String articleId = article.KnowledgeArticleId ;

   String language = ‘es_MX’; //es_mx is for spanish(mexico)

// assigned id can be the user or queue to  which this translations will be assigned for approval.

   String assigneeId = ’00G31000004VYZG’; //I am using hard-code one

// this is due date of translation approval

   Datetime dueDate = Datetime.newInstanceGmt(2015, 09,21);

  // submitForTranslation() function is used to submit the translation.

String id =KbManagement.PublishingService.submitForTranslation(articleId, language, assigneeId, dueDate);   

}

  

  }

global void finish(Database.BatchableContext BC) {

  

}

 

}

 

You can use this batch in multiple ways. For example, you can create a custom button, then redirect to a Visualforce page and pass the language for which you want to submit the translations. You can then call this batch from the Visualforce controller to submit the translations for more than one language.

 

Batch utility to update a custom field for all published support articles

 

There are several occasions when we may need to add a custom field on an article object, and so then we populate this field for all existing published articles. As we know, it is not simple to update an already published article, since they cannot be edited. In the below batch class, we will populate a custom field (Web_Product_date__c) for all already published support articles.

 

This batch class performs 3 actions to address the problem:

 

  1. It opens published articles in editing mode, which is equivalent to draft status.
  2. It then performs the updates on the articles that are now in draft status.
  3. Finally, it re-publishes all of the articles that are in draft status from step 2.

 

global with sharing class PopulateFieldOnPublishedArticlesBatch implements Database.Batchable <sObject>{

   global Database.QueryLocator start(Database.BatchableContext BC) {

    return Database.getQueryLocator([SELECT Id, ArticleNumber, KnowledgeArticleId FROM FAQ__kav WHERE PublishStatus = ‘online’ and language = ‘en_US’]);

}

global void execute(Database.BatchableContext BC, List<sObject> supportArticles){

   List<Id> articleIDs = new List<Id>();

    List<String> articleNumbers = new List<String>();

 

   //Step 1: Open published articles in editing mode which is Draft status

   for(FAQ__kav a: (List<FAQ__kav>)supportArticles){

   String Id = KbManagement.PublishingService.editOnlineArticle(a.KnowledgeArticleId, true);

        if (Id == null) {

       System.debug(‘##### ERROR While Editing’);

        }

        else{

        articleNumbers.add(a.ArticleNumber);

        }

      

   }

  

   List<FAQ__kav> articles = new List<FAQ__kav>();

    for (FAQ__kav d : [SELECT Id, KnowledgeArticleId FROM FAQ__kav WHERE PublishStatus = ‘draft’ AND Language = ‘en_US’ AND ArticleNumber IN :articleNumbers]) {

 

        // Step 2: perform your updates

        d.Web_Product_date__c = System.Now();

        articleIDs.add(d.KnowledgeArticleId);

        articles.add(d);

 

    }

   if(articles.size() > 0){

   update articles;

   }

   //Step 3: re-publish all the draft article of this batch.

   for (String articleId : articleIDs) {

        KbManagement.PublishingService.publishArticle(articleId, true);

    }

  

  }

global void finish(Database.BatchableContext BC) {

  

}

 

}

[/snippet]

I hope both of the article batch utilities help you automate the manual work on articles of your Salesforce org. We will keep you updated with more utility scripts in the future.

Previous Article
Integrating Microsoft Yammer and Salesforce
Integrating Microsoft Yammer and Salesforce

By Manmeet Manethiya In today’s business environments, both customers and employees are social. Bringing so...

Next Article
Upload a File as Large as 2GB in Salesforce Using a Visualforce Page
Upload a File as Large as 2GB in Salesforce Using a Visualforce Page

By Ashish Sharma Recently, we got a requirement in a project to upload a file into Salesforce that could be...