Not able to publish knowledge article via apex

apexknowledge-article

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.

try {
    // Retrieve Record Type ID for 'My_Record_Type'
    Id myRecordTypeId = Schema.SObjectType.Knowledge__kav.getRecordTypeInfosByDeveloperName().get('My_Record_Type').getRecordTypeId();

// Fetch online articles with null RecordTypeId
List<Knowledge__kav> articles = [
    SELECT KnowledgeArticleId 
    FROM Knowledge__kav 
    WHERE RecordTypeId = null AND PublishStatus = 'Online'
];

// Process each article
for (Knowledge__kav article : articles) {
    // Transition the online article to draft
    String draftArticleId = KBManagement.PublishingService.editOnlineArticle(article.KnowledgeArticleId, true);

    // If transition successful, fetch the draft article
    if (String.isNotEmpty(draftArticleId)) {
        Knowledge__kav draftArticle = [
            SELECT Id, KnowledgeArticleId 
            FROM Knowledge__kav 
            WHERE Id = :draftArticleId
        ];

        // Update the record type of the draft article
        draftArticle.RecordTypeId = myRecordTypeId;
        update draftArticle;

        // Republish the updated article
        KBManagement.PublishingService.publisharticle(draftArticle.KnowledgeArticleId, true);
    }
}
} 
catch (Exception e) {
    System.debug('Error processing articles: ' + e.getMessage());
    System.debug('Stack Trace: '+ e.getstackTracestring());
}