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.

Result of invoking a line 5.

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

Resources
- https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/apex_KbManagement_PublishingService_editOnlineArticle.htm
- https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/apex_KbManagement_PublishingService_publishArticle.htm
Was it helpful? Check out our other great posts here.
I need to update just over 1,700 articles (unpublish, update, publish), I am getting a CPU limit trying to unpublish those articles en mass. Could you point me to some ways to batch these, I am more of an admin that a dev.
This was an awesome post that helped me. Thank you very much.
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.
Great content! Super high-quality! Keep it up! 🙂