Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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 
     }                                        


  }
share|improve this answer
    
He is querying only online articles . – Mohith Shrivastava Mar 25 at 17:25
    
    
@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. – Antonio Manente Mar 25 at 17:36
    
I just upvoted this answer .Strange that query dont filter out draft articles . – Mohith Shrivastava Mar 25 at 17:38

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); 
share|improve this answer
    
Hey, thanks for your response. I verified that some of the records failed in the code, causing it to throw the exception. – Antonio Manente Mar 25 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 – Mohith Shrivastava Mar 25 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. – Antonio Manente Mar 25 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 ... – Mohith Shrivastava Mar 25 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.