In continuation of my effort to make Lightning Knowledge easily understandable for all, today I am going to show how Salesforce Knowledge can be accessed using Apex. Salesforce Knowledge can have Apex Triggers and be accessible through Apex code. Actually, Salesforce provides some standard apex classes related to Knowledge Management that can be called for actions like publishing, editing as draft, archiving, and more.

Before I start, I would recommend you going through my earlier posts –

We can use the methods in KbManagement.PublishingService class to manage the lifecycle of an article. I am going to explain some of the few classes which developer needs mostly, but to know about all the methods, I highly recommend you going through the documentation here.

Editing an Article as Draft

We can use the below method to edit an article as draft – 

String newId = KbManagement.PublishingService.editOnlineArticle(
                    knowledgeArticleId,
                    false
               );

Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).
2nd parameter is a Boolean value where false will not unpublish the already published articles and true will unpublish the already published article.
This method will return the new draft master version ID of the article.
Note – We need to keep this in mind that this method is not bulkified. So if you are trying to bulk edit a lot of published articles, this method will fail as it will throw Salesforce Limit. So the workaround is to implement Queueable class like below, where you call the queueable method with the batch size of your choice.
Queueable Class –

public class KnowledgeEditQueue implements Queueable {
List<Knowledge__kav> allKnowledges = new List<Knowledge__kav>();

public KnowledgeEditQueue(
List<Knowledge__kav> allKnowledges
) {
this. allKnowledges = allKnowledges;
}

public void execute(QueueableContext qc) {
List<String> newVersionIds = new List<String>();
String newVersionId;

for (Knowledge__kav single : allKnowledges) {
newVersionId = KbManagement.PublishingService.editOnlineArticle(
single.KnowledgeArticleId,
false
);
newVersionIds.add(newVersionId);
}
}

}

Calling the Queueable Method –
if (allArticles.size() > 0) {
    Integer count = 0;
List<Knowledge__kav> scope = new List<Knowledge__kav>();

for (Knowledge__kav single : allArticles) {
    if (count < BATCH_SIZE) {
    count++;
scope.add(single);
} else if (count == BATCH_SIZE) {
count = 0;
System.enqueueJob(new KnowledgeEditQueue(scope));
scope = new List<Knowledge__kav>();
scope.add(single);
}
}
if (!scope.isEmpty()) {
    System.enqueueJob(new KnowledgeEditQueue(scope));
}
}

Publish an Article 

We can use the below method to publish an article  – 

KbManagement.PublishingService.publishArticle(
      knowledgeArticleId,
      false
);

Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).
2nd parameter is a Boolean value where false will not publish the article as a new version, whereas true will publish the article as a new version.
This method is not bulkified, so you can use the same mechanism of Queueable class to bulk publish articles.

Schedule Article Publication

We can use the below method to schedule article publication – 

Datetime scheduledDate = Datetime.newInstanceGmt(2012, 12,1,13,30,0);
KbManagement.PublishingService.scheduleForPublication(
      knowledgeArticleId,
      scheduledDate
);

Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).
2nd parameter is the schedule date (If nothing is specified, then the article will be published immediately).


Delete Draft Article 

We can use the below method to delete draft article – 

KbManagement.PublishingService.deleteDraftArticle(
      knowledgeArticleId
);

Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).

Archive Online Article

We can use the below method to archive online article – 

Datetime scheduledDate = Datetime.newInstanceGmt(2012, 12,1,13,30,0);
KbManagement.PublishingService.archiveOnlineArticle(
      knowledgeArticleId,
      scheduledDate
);

Where the 1st input parameter is the KnowledgeArticleId (Note – This is not the Knowledge Record Id).
2nd parameter is the schedule date (If nothing is specified, then the article will be archived immediately).

Trigger Consideration for Knowledge Articles

ActionWhich Trigger Context
New Article SavedBefore Insert and After Insert
Existing Draft Articles Saved AgainBefore Update and After Update
Publish ArticleNo
Archive ArticleNo
Edit as DraftBefore Insert and After Insert
Undelete Article From Recycle BinNo
Delete ArticleBefore Delete and After Delete
AssignBefore Update and After Update (Only when the article is edited before being assigned)
Importing ArticlesBefore Insert and After Insert
Importing Articles with TranslationBefore Update and After Update

I hope this will help you when dealing with Knowledge from Apex. If you have any feedback, please post it in the comment section. Thank you.