I have a requirement to create a new record type in the org and assign it to all published knowledge articles.
I have created record type My Record Type in the org and updating all records via anonymous apex.
The below snapshot is trying to update a single published knowledge article.
If this works I will extend it to update all knowledge articles.
try
{
List<Knowledge_kav> knowledgeRecords = [SELECT Id, knowledgeArticleid FROM Knowledge_kav
WHERe RecordTypeId = null
AND PublishStatus = 'Online'
AND id = 'somePublishedKnowledgeArticleId'];
Id myRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByDeveloperName().get('My_Record_Type').getRecordTypeId();
String newArticleId;
Knowledge__kav draftKnowldegeArticle;
newArticleId = KBManagement.PublishingService.editOnlineArticle(knowledgeRecords[0].KnowledgeArticleId
, false);//Updating the Knowledge article to Draft
draftKnowldegeArticle = [SELECT Id, knowledgeArticleid FROM Knowledge__kav
WHERe RecordTypeId = null AND id = :newArticleId];
Knowledge__kav knowledgeRecordsToUpdate = [SELECT Id, knowledgeArticleid
FROM Knowledge__kav
WHERE knowledgeArticleId = :draftKnowldegeArticle.KnowledgeArticleId
LIMIT 1];
knowledgeRecordsToUpdate.RecordTypeId = myRecordTypeId;
update knowledgeRecordsToUpdate;
KBManagement.PublishingService.editOnlineArticle(knowledgeRecordsToUpdate.KnowledgeArticleId,true);//Getting error here
}
catch(Exception exceptionInstance)
{
System.debug('Error message=' + exceptionInstance.getMessage();
System.debug('Stack trace=' + exceptionInstance.getStackTraceString());
}
But getting error message like below:
Error message=You can't perform this action. Be sure the action is valid for the current state of the article, and that you have permission to perform it.
Does anyone have any idea about it?
Best Answer
Answer There were 2 problems with the code.