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.

Written by

Related Posts

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

New Apex Method | Extracting Picklist Values Based on Record Type Inside Apex

For years, Salesforce developers have faced a common challenge: programmatically retrieving picklist values that are specific to a certain Record Type directly within Apex. The “solutions” often involved either complex SOQL queries against metadata, relying on the UI API (with its associated callout limits and serialization overhead), or maintaining clunky custom metadata/settings.

Good news, Technical Potpourri readers! The Salesforce Spring ’26 release is bringing a game-changer that will significantly simplify your Apex code and improve performance: native Apex methods to filter picklist values by Record Type!

read more...
Apex Cursor and Its Advantages Over Batch Processing

Apex Cursor and Its Advantages Over Batch Processing

Struggling with large datasets and SOQL governor limits in Salesforce? In this video, I will dive deep into Apex Cursors, the powerful new feature that revolutionizes how we process millions of records.

Apex Cursors allow developers to navigate through query results in manageable chunks within a single transaction or across Queueable jobs. In this video, I will show you the correct way to implement Apex Cursor and also when this is an exception, how to handle that and perform retry.

read more...

3 Comments

  1. Sakshi

    Hi! Can we create a custom vf page to display queried knowledge articles? Thanks in advance.

    Reply
  2. Sudipta Deb

    Absolutely you can. But there are better way to display knowledge articles through flow (declarative way). In my next post, I am going to show how to do Knowledge operations in Flow.

    Also there is a post few weeks back where I showed how you can use the Salesforce Lab DataGrid solution to display records in flow. You can have a look at – https://www.sudipta-deb.in/2020/08/show-your-data-in-datatable-map-or-tile.html

    Reply
  3. Unknown

    I also have the use case of conditionally displaying a specific knowledge article based upon a toggle being true if a Screen flow. Is this possible?

    Reply

Leave a Reply