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
aparna d 1aparna d 1 

Batch archive articles test class

Hi Everyone am facing issues with archive article test class and can't able to findout the solution. If any one have idea on this please help me.Thanks in advance

global class BatchArchiveKB implements Database.Batchable<SObject> {
    global string query;

    global BatchArchiveKB() {
        Map<String, ArchiveKB__mdt> mapCMD = ArchiveKB__mdt.getAll();
    
    }

    global Database.QueryLocator start(Database.BatchableContext BC){
         ArchiveKB__mdt[] KBids = [Select Id, Salesforce_ID__c from ArchiveKB__mdt];
          Set<String> sfid = new Set<String>();
              for(ArchiveKB__mdt record:KBids) {
                  sfid.add(record.Salesforce_ID__c);
            }
        System.debug('sfids'+sfid);
         return Database.getQueryLocator([Select Id, KnowledgeArticleId FROM Customer_Support__kav WHERE ID =:sfid]);
  
    }
            
    global void execute(Database.BatchableContext BC, List<Customer_Support__kav> todelete) {
        system.debug('Todelete'+todelete);
        for (Customer_Support__kav kav:  todelete)
        {
            KbManagement.PublishingService.archiveOnlineArticle(kav.knowledgeArticleId,null);
             System.debug(kav.Id);
        }   
       
    }
     global void finish(Database.BatchableContext bc){
    
    }

}

Test class
-----------------------------------
@isTest
public class BatchArchiveKB_test {
    
    @testVisible static List<ArchiveKB__mdt> articlemap{ 
        get{ 
            if(articlemap == NULL){ 
                articlemap = [SELECT Id, Salesforce_ID__c FROM ArchiveKB__mdt]; 
            } 
            return articlemap; 
        } 
        set; 
    } 
    
    @istest
    public static void testBatchArchive(){
        
        User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
        System.runAs (thisUser) 
        {
          Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
          User u = new User(Alias = 'standt', Email='test@test.com', 
            EmailEncodingKey='UTF-8', LastName='Testing',  
            LocaleSidKey='en_US', ProfileId = p.Id, 
             UserName='sdss123@test.com');

        
        Customer_Support__kav kv = new Customer_Support__kav();
        kv.Title='test';
        kv.urlname='testurl';    
        kv.Summary= 'test';
        kv.IsVisibleInPkb=true;
        kv.IsVisibleInCsp=true;
        kv.IsVisibleInPrm =true;
        // kv.PublishStatus ='online'
        //kv.language='en_US';
        insert kv;
               
        Test.startTest();
        Customer_Support__kav kbs = [Select KnowledgeArticleId from Customer_Support__kav where ID = :kv.Id];
        string articleid = kbs.KnowledgeArticleId ;
      
        KbManagement.PublishingService.archiveOnlineArticle(articleid, null);
        
       
        BatchArchiveKB bc = new BatchArchiveKB ();
        Database.executeBatch(bc,200);
        
        
        Test.stopTest();
    }
    
    }    
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aparna,

Can you confirm what was the exact issue in your test class so experts can suggest you based on it. The question as of now is such that experts need to implement and check what excatly the issue is. If you would share the exact information it would be easy.

Thanks,
 
aparna d 1aparna d 1
Hi Praveen am getting the below error when i run the test class.

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.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Aparna,

Can you check the below question from stack exchange with the same error and solution for the same.

https://salesforce.stackexchange.com/questions/115592/mass-updating-published-articles

I hope this should resolve the issue. If this solution helps, please mark it as best answer.

Thanks,
 
aparna d 1aparna d 1
Thank you praveen its working