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
VIGNESH BALASUBRAMANIAN 27VIGNESH BALASUBRAMANIAN 27 

Cannot move Published article to draft via apex.

Hi Everyone,

When I try to move published article to draft using this code , 
String str =KbManagement.PublishingService.editOnlineArticle('kAA4C000000CaTmWAK', true);, it throws following error.
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'm runing this code as system admin who have "Manage Article Permission".

Can you guys get me a solution for this.

Thanks,
Vignesh.B
NagendraNagendra (Salesforce Developers) 
Hi Vignesh,

sorry for this issue you are facing.

May I suggest you please double check with the following permissions is any one of them might be missing.
  • To create articles: “Manage Articles” AND "Create” and “Read” on the article type 
  • To edit draft articles: “Manage Articles” AND “Read” and “Edit” on the article type 
  • To edit published or archived articles: “Manage Articles” AND “Create,” “Read,” and “Edit” on the article type 
They also have Article Actions under Setup for Edit Published and Archived Knowledge Articles, which is set to ALL 

Also, could you please confirm what you have in "string str"? because edit online article will take the parameter article ID as string ID.

Happy to help further.

Please let us know if this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
Raj VakatiRaj Vakati
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);
Piotr GajekPiotr Gajek

Hi Vignesh,

You received this error because you are using not correct Id. Try to use KnowledgeArticleId, not a Knowledge__kav.Id.

You can find more details here (https://salesforceprofs.com/updating-knowledge-articles-programmatically-by-apex/).

[SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticle.Id].KnowledgeArticleId;
@AuraEnabled
    public static String unPublishArticle(String recordId){ //It need to be KnowledgeArticleId
        String newArticleId = KbManagement.PublishingService.editOnlineArticle(recordId, true); 
        return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticleId].KnowledgeArticleId;
    }


Regards,

Piotr

Mitesh SuraMitesh Sura
Hi Piotr and Raj, 
We are running into same issue. We checked all the permissions. And we are passing the right KA id. Interesting thing is, the exact same logic works for some articles of same type. Is there anything else we are over looking? 
Piotr GajekPiotr Gajek

Hi Mitesh,

It can have a few causes:

  1. You are not using KnowledgeArticleId 
  2. Your article is saved as a draft and archived. Try the query below and to be sure that the 'articles' list has only one element. 
Knowledge__kav myArticle = [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: 'myArticleId' ];

List<Knowledge__kav> articles = [SELECT Id, PublishStatus, KnowledgeArticleId FROM Knowledge__kav WHERE  KnowledgeArticleId =: myArticle.KnowledgeArticleId ];

     3. Each time when you invoke KbManagement.PublishingService.editOnlineArticle  => KnowledgeArticleId is changing.

String newKnowledgeArticleId = KbManagement.PublishingService.editOnlineArticle(recordId, true); 
Knowledge__kav newArticle = [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newKnowledgeArticleId ];


If it still doesn't work please give me more details (like code snippet, article PublishStatus), I will try to help. 

Mitesh SuraMitesh Sura
Hi Piotr,
Thanks for quick response. I think we may be running into "one or more of those online article already has a draft article" issue mentioned here: https://salesforce.stackexchange.com/questions/115592/mass-updating-published-articles

Here is the code snippet
 
List<SObject> articlesList = 'SELECT ArticleType, Id, KnowledgeArticleId, PublishStatus, IsLatestVersion' +
            ' FROM KnowledgeArticleVersion' +
            ' WHERE PublishStatus = \'Online\' AND IsLatestVersion = True';

for (SObject articleRecord : articlesList) {
  try {
    Id unpublishedId = KbManagement.PublishingService.editOnlineArticle(String.valueOf(articleRecord.get('KnowledgeArticleId')), false);
    unpublishedArticlesMap.put(unpublishedId, articleRecord);
  } catch (Exception e) { }
}

 
pratik kanetkarpratik kanetkar
Hi Vignesh,
   You are using 18 digit code(Salesforce Id) to edit it because of that it will get that error please make 15 digit code (Salesforce Id), It will work.
I have same problem but now working good.

Thanks,