function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ArmouryArmoury 

System.handledException for Knowledge Articles in Test Class

Hello,

I have a piece of test code to create and publish the articles and it goes as below..
 
Product_Info__kav  testArticle = new Product_Info__kav ();
testArticle .Title = 'Test Prod Info';
testArticle .Summary = 'Test Summary';
testArticle .URLName = 'Test Prod Info';
insert testArticle ;

Product_Info__kav insertedTestArticle = [SELECT KnowledgeArticleId FROM Product_Info__kav WHERE ID = :testArticle.Id];
            
KbManagement.PublishingService.publishArticle(insertedTestArticle.KnowledgeArticleId, true);  

The issue is this test code works fine in 3 sandboxes but it is failing in one particular sandbox and I am getting the below exception in Line #9.
 
System.HandledException: 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.

I checked the knowledge settings, permission set access for the article types and everything looks fine but unable to find out why this error is happening and that too for just one sandbox. Any help is appreciated
Kaushal PitrodaKaushal Pitroda
Hi Armoury,

It is too late for a reply but I am posting anyway so if any other users are getting the same error will get some help.

I was getting the same error while validating change set in production and resolved it by following below steps.

Set IsVisibleInCsp, IsVisibleInPkb and IsVisibleInPrm true in Product_Info__kav.
Remove line no. 9 KbManagement.PublishingService.publishArticle.

Your code will look like below.
 
Product_Info__kav  testArticle = new Product_Info__kav ();
testArticle.Title = 'Test Prod Info';
testArticle.Summary = 'Test Summary';
testArticle.URLName = 'Test Prod Info';
testArticle.IsVisibleInCsp = true;
testArticle.IsVisibleInPkb = true;
testArticle.IsVisibleInPrm = true;
insert testArticle ;

Product_Info__kav insertedTestArticle = [SELECT KnowledgeArticleId FROM Product_Info__kav WHERE ID = :testArticle.Id];
            
//KbManagement.PublishingService.publishArticle(insertedTestArticle.KnowledgeArticleId, true);

By this, your article will be shown published when you query it like you are doing at line no. 7.