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
trailhead solutions 10trailhead solutions 10 

Need Code to write in Batch Apex

Hello All

I am trying to execute the below code from Anonymous window to change Published articles to Draft Articles.
While Executing, I came across with few errors. So to handle that errors skip and continue for the Iteration, I tried with try-catch block. After that, when number increased, it is showing error as --too many soql queries-101. So to handle this error, '@future' cant be written and used in local, it showing.So I cant manually click the execute button multiple times as there are 35k records to change to draft status.
So, request to please someone help to write this code in Batch apex, so that it divides and runs automatically for 35000 records.

for(integer i=0;i<=3;i++){
       
List<String> NewdraftList = new List<String>();
list<Knowledge__kav> publishlist=[SELECT Id,title, ArticleNumber, KnowledgeArticleId,IsVisibleInPkb FROM Knowledge__kav WHERE PublishStatus = 'Online'
limit 25];
for(Knowledge__kav k :publishlist){
try{ 
String newDraftArticleId = KBManagement.PublishingService.editOnlineArticle(k.KnowledgeArticleId,true);
NewdraftList.add(newDraftArticleId);       
}
 catch(exception e){
    system.debug('The records failed to process:'+e.getmessage());
 }   
}   
     
}


Thanks
GSN
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi GSN,

Is the query which you used is having limit 25 to elimate SOQL issue. If so you can use that query in start method and remove the limit 20. and add the remaining code in the execute method . 

Below is the example for the batch class. Let me know if you need any further help on it.

https://www.sfdcpoint.com/salesforce/batch-apex-example-in-salesforce/

If this solution helps, Please mark it as best answer.

Thanks,