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
Kasia Wojewodzka 7Kasia Wojewodzka 7 

Cannot move Published article to draft via apex. Error

Hi I am trying to mass update Knowledge Articles from Published to Drafts  (Developer Console: Debug> Execute Anonymos Windwow) 
Its been working succesfully with smaller updates until when I got about 1600 articles to update.
List<Knowledge__kav> articleIds = [SELECT Id,Region__c,LastPublishedDate,Line_of_business__c,Title, KnowledgeArticleId FROM Knowledge__kav WHERE PublishStatus = 'Online' AND Title <> 'Digital Cytology: Slide Scanning' AND Title <> 'SNAP Tests: Universal SNAP Protocol Checklist' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'This is a test of LOB and Region' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'Cornerstone: Lab results run under the wrong patient - wrong patient on lab report PDF' AND  Line_of_business__c ='' AND  Language = 'en_US' WITH DATA CATEGORY Line_of_Business__c AT (IHD__c) LIMIT 100];
for(Knowledge__kav article : articleIds){
KBManagement.PublishingService.editOnlineArticle(article.KnowledgeArticleId, true);
}
system.Debug(articleIds.size());



I am getting the follwoing 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 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

I am new to apex code.  Would you advise how to modify the exsiting code i have to use Try and Catch ? Thank you in advance for your help. 

Kasia 

 
Best Answer chosen by Kasia Wojewodzka 7
ShivankurShivankur (Salesforce Developers) 
Hi Kasia,

This error 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).

You can modify your existing code like below to avoid the error and catch it in your apex code:
for(Knowledge__kav k : [SELECT Id,Region__c,LastPublishedDate,Line_of_business__c,Title, KnowledgeArticleId FROM Knowledge__kav WHERE PublishStatus = 'Online' AND Title <> 'Digital Cytology: Slide Scanning' AND Title <> 'SNAP Tests: Universal SNAP Protocol Checklist' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'This is a test of LOB and Region' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'Cornerstone: Lab results run under the wrong patient - wrong patient on lab report PDF' AND Line_of_business__c ='' AND Language = 'en_US' WITH DATA CATEGORY Line_of_Business__c AT (IHD__c) LIMIT 100]){
         try{
             String onlineK=KBManagement.PublishingService.editOnlineArticle(k.KnowledgeArticleId,true);
        }
     catch(exception e){
         //nothing to do here, draft existed 
     }                                       

  }

Since, you are new to Apex code, I would recommend you learning the usage of Try Catch to handle the exceptions.

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_statements.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.

All Answers

ShivankurShivankur (Salesforce Developers) 
Hi Kasia,

This error 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).

You can modify your existing code like below to avoid the error and catch it in your apex code:
for(Knowledge__kav k : [SELECT Id,Region__c,LastPublishedDate,Line_of_business__c,Title, KnowledgeArticleId FROM Knowledge__kav WHERE PublishStatus = 'Online' AND Title <> 'Digital Cytology: Slide Scanning' AND Title <> 'SNAP Tests: Universal SNAP Protocol Checklist' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'This is a test of LOB and Region' AND Title <> 'SNAPShot Dx: Streaking and/or Speckling' AND Title <> 'Cornerstone: Lab results run under the wrong patient - wrong patient on lab report PDF' AND Line_of_business__c ='' AND Language = 'en_US' WITH DATA CATEGORY Line_of_Business__c AT (IHD__c) LIMIT 100]){
         try{
             String onlineK=KBManagement.PublishingService.editOnlineArticle(k.KnowledgeArticleId,true);
        }
     catch(exception e){
         //nothing to do here, draft existed 
     }                                       

  }

Since, you are new to Apex code, I would recommend you learning the usage of Try Catch to handle the exceptions.

Reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_trycatch_example.htm
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_exception_statements.htm

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
This was selected as the best answer
Kasia Wojewodzka 7Kasia Wojewodzka 7
Thank you Shivankur ! The modified code worked like a charm.  I will review the links with the resources you provided to get myself familiar with Try Catch to handle the exceptions. Your help is much appriciated.