Updating Knowledge Articles programmatically by Apex?

Hi Devs,
Today I wanna show you how we can in an easy way:
– save
– update
– publish
– unpublish articles by Salesforce Apex.

Introduction of articles methods

I used a two methods to accomplished publishing and update by apex.

publishArticle(articleId, flagAsNew)
If flagAsNew is set to true, the article is published as a major version.

editOnlineArticle(articleId, unpublish)
Creates a draft article from the online version and returns the new draft master version ID of the article. Also, unpublishes the online article, if unpublish is set to true.

Apex Code

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); 
        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;
   }

}

Example of articles use

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);

Result

Result of invoking lines 1 and 2.

draft articles

Result of invoking a line 5.

published articles

Result of invoking lines 8,9,10 or 13.

updated articles

Resources

Was it helpful? Check out our other great posts here.

5 2 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Walt K
Walt K
28 days ago

This was an awesome post that helped me. Thank you very much.

Harsha
Harsha
1 year ago

Shouldn’t line 33, return two rows because in the WHERE condition you are using knowledgearticleID instead of articleID. It should return both originally created article and the newly created draft.

AffiliateLabz
1 year ago

Great content! Super high-quality! Keep it up! 🙂

Close Menu
3
0
Would love your thoughts, please comment.x
()
x