3

How to create a new knowledgearticle object from Apex code in salesforce? Using the insert statement with a knowledgearticle shows a DML operation INSERT not allowed on KnowledgeArticleVersion error.

Also, if there is an already exisiting article...how to update the article .. For e.g. add some more text to the summary of the knowledge artilcle.

1

3 Answers 3

3

I know that it was a few years ago, but here is my code snippet for future usage. I used publishArticle(articleId, flagAsNew) and editOnlineArticle(articleId, unpublish).

You can find more details on my blog here.

public with sharing class ArticlesUtils {

@AuraEnabled
public static List<Knowledge__kav> getAllArticles(){
    return [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav ];
}

@AuraEnabled
public static String createNewArticleAsADraft(String title, String urlName) {

    Knowledge__kav newArticle = new Knowledge__kav();
    newArticle.Title = title;
    newArticle.UrlName = urlName;
    insert newArticle;

    return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticle.Id].KnowledgeArticleId;
}

@AuraEnabled
public static void publishArticle(String recordId) { //It need to be KnowledgeArticleId
    KbManagement.PublishingService.publishArticle(recordId, true);
}

@AuraEnabled
public static String unPublishArticle(String recordId){ //It need to be KnowledgeArticleId
    String newArticleId = KbManagement.PublishingService.editOnlineArticle(recordId, true); //Method new version id use it to update 
    return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticleId].KnowledgeArticleId;
}

@AuraEnabled
public static String updateDraftArticleWithoutPublish(String title, String urlName, Id recordId) {

    Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: recordId ];   

    newArticle.Title = title;
    newArticle.UrlName = urlName;

    update newArticle;

    return newArticle.KnowledgeArticleId;
}

@AuraEnabled
public static String updatetArticle(String title, String urlName, Id recordId) {

    String newVersionId = unPublishArticle(recordId);

    Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: newVersionId ];   

    newArticle.Title = title;
    newArticle.UrlName = urlName;

    update newArticle;

    publishArticle(newVersionId);

    return newVersionId;
}
}

Usage example:

List<Knowledge__kav> articles = ArticlesUtils.getAllArticles();

String newArticleKnowledgeId = ArticlesUtils.createNewArticleAsADraft('SalesforceProfs', 'salesforce-profs');

ArticlesUtils.publishArticle(newArticleKnowledgeId);

//unpublish, update, publish > separate actions
String newArticleVersionId = ArticlesUtils.unPublishArticle(newArticleKnowledgeId);
ArticlesUtils.updateDraftArticleWithoutPublish('SalesforceProfs Update', 'salesforce-profs-update', newArticleVersionId);
ArticlesUtils.publishArticle(newArticleVersionId);

//update - contain unpublish, update, publish
//ArticlesUtils.updatetArticle('SalesforceProfs Update', 'salesforce-profs-update', newArticleKnowledgeId);
1

Only way is through the UI or Import Wizard. The KnowledgeArticle object does not support create() per documentation: http://www.salesforce.com/us/developer/docs/api/index_Left.htm#CSHID=sforce_api_erd_support.htm|StartTopic=Content%2Fsforce_api_erd_support.htm|SkinName=webhelp

1

First of all, if you create an article type, Salesforce automatically generates a set of Objects that you can manipulate via Apex DML. For instance, if you created an article type "Standard_Article", then there will be a somehow virtual table "Standard_Article__kav" and you can treat this table as a regular Salesforce table and access it via DML.

Standard_Article__kav article = new Standard_Article__kav();
article.Summary = tempKnowledge.Summary__c;
article.Title = tempKnowledge.Name;
article.UrlName = tempKnowledge.Name.replace(' ','-');
article.Language = 'de';
database.insert(article);

Three things to consider:

  1. You will have to set a language.
  2. You will have to initialize the urlname and this field must be unique for all articles, regardless if published or not.
  3. The worst: The user that runs the trigger needs a knowledge license.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .