8

I have ~1000 Knowledge Articles that need to be updated. I've come across this class to accomplish this programmatically. Although I'm getting the following error every time I try to call one of these methods.

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.

This is the code that generated the error:

for(Article_Type__kav k : [SELECT Id, ArticleNumber, KnowledgeArticleId 
                             FROM Article_Type__kav 
                            WHERE PublishStatus = 'Online' 
                              AND Language = 'en_US']){
String onlineK=KBManagement.PublishingService.editOnlineArticle(k.KnowledgeArticleId,false);
}

I've tried the same using the KnowledgeArticleVersion and filtering by type as well. Same error. I'm running this as a system administrator and am able to create/edit/update/delete/publish articles through the UI as well. Is there a permission I'm missing to utilize the PublishingService class? Any help is greatly appreciated.

3 Answers 3

6

This method creates draft articles from online articles, so this happens because one or more of those online article already has a draft article so you cannot create another draft article(you cannot have 2 drafts for a single article at the same time).

This should work for you

    for(FAQ__kav k : [SELECT Id, ArticleNumber, KnowledgeArticleId
                      FROM FAQ__kav
                      WHERE PublishStatus = 'Online'  AND Language = 'en_US']){
         try{
             String onlineK=KBManagement.PublishingService.editOnlineArticle(k.KnowledgeArticleId,false);
        }
     catch(exception e){
         //nothing to do here, draft existed 
     }                                        


  }
8
  • He is querying only online articles . Commented Mar 25, 2016 at 17:25
  • Let us continue this discussion in chat. Commented Mar 25, 2016 at 17:26
  • @PepeFloyd I suspect you are correct as I tested this in a Sandbox org where there were already draft versions of some of the articles I was trying to edit. I ended up wrapping the method call in a try/catch and was able to see the cause. Thanks for your help. Commented Mar 25, 2016 at 17:36
  • 1
    I just upvoted this answer .Strange that query dont filter out draft articles . Commented Mar 25, 2016 at 17:38
  • Why is it strange that it isn't filtering out published articles that already have a draft? No condition has been set to indicate that this query should do so. The query is simply finding published articles...
    – bpilling
    Commented Mar 24, 2020 at 2:51
2

The issue may be caused due to

1.You dont have knolwedge User Checked on your user .From your description of problem I am ruling this out since you say from UI you are able to do this .But just double check your user record to see if knowledge User is checked

2.Ensure proper permissions like

“Manage Articles” (This permission is on by default in the System Administrator profile.)

AND

“Read”, “Create”, and “Edit” on the article type

AND

A publish or archive article action, set on the Article Actions Setup page

Also note that if one of records fail in your code ,all the records fail ,try doing with hardcoded Id first one by one

KBManagement.PublishingService.editOnlineArticle('put your harcode Id once',false); 
5
  • Hey, thanks for your response. I verified that some of the records failed in the code, causing it to throw the exception. Commented Mar 25, 2016 at 17:34
  • Use those Ids now to figure why they error out .May be those are locked for you to not edit .Also check below answer from Pepeflyod .Are those draft articles ? Please share your further findings Commented Mar 25, 2016 at 17:36
  • I commented on Pepefloyd's answer. He was correct. I tested this in a Sandbox org where there were already draft versions of some of the articles I was trying to edit. That was the cause of the exception. Commented Mar 25, 2016 at 17:38
  • 1
    Great !!Its weird that your query dont filter draft articles .I think now you need to check for all article versions and query only those that have no draft article version in them ... Commented Mar 25, 2016 at 17:40
  • @MohithShrivastava I am trying something similar here salesforce.stackexchange.com/questions/309841/… We need to update more than 1000 records through data loader. I created a trigger on the Knowledge which changes the article body, am using the KbManagement.PublishingService to unpublish the article update them and republish. When I pass the ID to dataloader it throws error. Can you please suggest me if I am missing anything
    – user81642
    Commented Jun 18, 2020 at 2:53
0

Another cause for this error can be when a draft article is currently in the middle of an approval process. If you try to delete the draft article using Apex when there is a pending approval (and possibly when the approval process locks the record) you may experience the same error message.

Simply approve or reject the draft before deleting it, or run a script to reject all approvals in flight.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .