Apex Tip: Create a Knowledge Base Article from Apex

Backgound

If you worked with Salesforce Knowledge Base Articles in Apex, I would know that so many objects and restrictions that make that feature really difficult to rich a deep customization. 

As you can read in the official Doc you have to handle many tables related to Articles:
  • Article Type__DataCategorySelection Object A data category selection represents a data category that classifies an article.
  • Article Type__ka Object Gives access to an article from a specific article type independent of its version. This object is read–only and can’t be used in a SOQL clause or in a WITH DATA CATEGORY DataCategorySpec SOSL clause. For more information, see KnowledgeArticle.
  • Article Type__kav Object Gives access to all articles from a specific article type depending on their version. This object gives access to the fields available in KnowledgeArticleVersion. For more information, see KnowledgeArticleVersion.
  • Article Type__Feed Object Represents a single feed item in the feed displayed on the detail page for an article.
  • Article Type__ViewStat Object Provides statistics on the number of views for an article from a specific article type. For more information, see KnowledgeArticleViewStat.
  • Article Type__VoteStat Object Provides the weighted rating for an article from a specific article type on a scale of 1 to 5. For more information, see KnowledgeArticleVoteStat.
  • CaseArticle Object Represents the association between a Case and a KnowledgeArticle.
  • FeedComment Object Represents a comment added to a feed by a user.
  • FeedItem Object FeedItem represents an entry in the feed, such as changes in a record feed, including text posts, link posts, and content posts.
  • KnowledgeArticle Object Gives access to an article independent of its version. This object is read–only and can’t be used in a SOQL clause or in a WITH DATA CATEGORY DataCategorySpec SOSL clause.
  • KnowledgeArticleVersion Object Provides a global view of standard article fields across all article types depending on their version.
  • KnowledgeArticleViewStat Object Provides statistics on the number of views for the specified article across all article types.
  • KnowledgeArticleVoteStat Object Provides the weighted rating for the specified article on a scale of 1 to 5 across all article types.
  • NewsFeed Object Represents a single feed item on a user’s home tab. A Chatter feed shows recent changes to records that the user is following.
  • UserProfileFeed Object Represents a user profile feed, which tracks all actions by a user on records that can be tracked in a feed. This feed is displayed on the user profile page.
This week I had to create a REST web service to expose some FAQ articles to an external web site. That is not a big deal because you only have to query the ArticleType__kav object. The only tricky part there is to include the Language and PublishStatus in the Where clause.
Let me show you an example, if the ArticleType is FAQ, the query should be something like:

Create an Article from Apex.

After create the web service I had to create the Test Class (I really create it before ;)) I faced the problem to create the article from Apex.
After spend some time googling what table should I insert the article and what order I figured out that it is really easy. I only needed insert the article in FAQ__kav and SFDC trigger the insertion of the rest related records.
So, this is the TestHelper I made to create an Article:
More about the publishing service here.
Enjoy :)

4 thoughts on “Apex Tip: Create a Knowledge Base Article from Apex

  1. Castle defense 2 hack

    you are really a excellent webmaster. The website loading speed is incredible.
    It seems that you’re doing any distinctive trick.
    Moreover, The contents are masterpiece. you’ve performed a great task
    in this subject!

    Reply
  2. Praful Gadge

    Nice blog, useful while inserting articles in bulk.

    If someone wants to bulk follow these articles they can use the below script #GivingBack

    list subscriptions = new list();
    //keep 100 limit to avoid 10001 DML limit
    list kas = [ Select ID,KnowledgeArticleId from KnowledgeArticleVersion WHERE publishstatus='Online' and Language = 'en_US' LIMIT 100 ];
    list us = [ select id from User where IsActive=TRUE AND UserPermissionsKnowledgeUser=TRUE LIMIT 100 ];

    for(KnowledgeArticleVersion k : kas){
    for(User u : us){
    EntitySubscription es = new EntitySubscription(ParentId = k.KnowledgeArticleId, subscriberId = u.Id);
    subscriptions.add(es);
    }
    }
    insert subscriptions;

    Reply

Leave a Reply to Castle defense 2 hack Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">