Thursday, January 15, 2015

Create Knowledge Base Article, Publish It (Or Not), and Attach It To A Case

One best practice for Case Management (Service Cloud) is to attach a Knowledge Base Article when closing a case.

This code is for a circumstance where we want an external VF page to display the Case info and allow the user to enter the Knowledge Base Information that resolves the Case.  The KB info gets saved to SFDC.  Then the association is made between the KB Article and the case.

In our case, we did not want the Article published right away.  This allowed a second set of eyes to review the article, make modifications if necessary, and then publish it.  But the code below shows how to programatically publish the article.
  

public Case c { get; set; }

// QA_Corrective_Action__kav is an ArticleType.  Remember, each Knowledge Base Article gets its own object based on the ArticleType
public QA_Corrective_Action__kav CorrectiveAction { get; set; }

public PageReference updateCase() {
        
        //Clean up the URL a bit
        String UrlName = CorrectiveAction.title.replace(' ','-');
        UrlName = UrlName.replace('#','');
        UrlName = UrlName.replace('&','');
        UrlName = UrlName.replace('?','');
        CorrectiveAction.UrlName = UrlName ;

        //1st Create the article
        insert CorrectiveAction;

        KnowledgeArticleVersion kav = [Select Id, KnowledgeArticleId from KnowledgeArticleVersion  where PublishStatus = 'Draft' and Id=: CorrectiveAction.Id];
        
        //2nd Publish the article (if you want)
        //Comment Out Line Below if you do NOT want the Article published yet.
        KbManagement.PublishingService.publishArticle(kav.KnowledgeArticleId , true);

        
        CaseArticle cArticle = new CaseArticle();
        cArticle.CaseId = c.Id;
        cArticle.KnowledgeArticleId = kav.KnowledgeArticleId ;

        //3rd Create the association between the Article and the Case
        insert cArticle;
        
        
        return new PageReference('/apex/QACaseUpdateThankYou');
        
    }

No comments:

Post a Comment