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
JotmenowJotmenow 

Too many dml statements knowledge

Hi, Iam trying to create a draft version of published articles using below code. It worked well for handful of records. I hv a scenario to update 3000 records. I receive TOO many DML statement errors.

Any fix here will help.

Used code:

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 
     }                                        


  }
 
Best Answer chosen by Jotmenow
JotmenowJotmenow
Brahmajit/Amit,
Appreciate your response.
I tried creating a list only to find new error that knowledge articles object ending with __kav are not Sobjects.

So,list concept would not work.

I found a workaround which would work for 1-4000 records.

List of asyncronous code as below.
Increare offset by 150 each time and execute till closure.

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


  }

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


  }

 

All Answers

brahmaji tammanabrahmaji tammana
Hi,

You need to bulkify your code to avoid this governor limit error.
Avoid writing DML statements inside the loops and use collections to perform DML operation on bulk records.

Thanks
Brahma
Amit Singh 1Amit Singh 1
Hello,

You can use Apex Batch to avoid the Error use below link for the reference:
http://salesforce.stackexchange.com/questions/9220/how-to-mass-archive-articles-in-apex

Let me know if this helps :)

Thanks!
Amit Singh
JotmenowJotmenow
Brahmajit/Amit,
Appreciate your response.
I tried creating a list only to find new error that knowledge articles object ending with __kav are not Sobjects.

So,list concept would not work.

I found a workaround which would work for 1-4000 records.

List of asyncronous code as below.
Increare offset by 150 each time and execute till closure.

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


  }

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


  }

 
This was selected as the best answer