[SalesForce] Publishing Article in Test Class

I am having trouble with the publishing of Knowledge Article Versions(KAV) in a test class. I tried to use the PublishingService.publishArticle but I could not get it to work. It gave me the following error:System.InvalidParameterValueException: Invalid ID.

The important part of my code:

@AuraEnabled
public static List<wrapperClass> getsubTopicList(String subtopic)
{
    List<wrapperClass> lstWrapper = new List<wrapperClass>();
    List<Video_Article__kav> newsKAList = new List<Video_Article__kav>();
    try
    {
        newsKAList = [SELECT Id, ArticleNumber, Body__c ,LastPublishedDate, Steps__c, Sub_Topic_Step_by_Step__c, Rich_Summary__c, Topic__c, Summary, CreatedBy.Name, Title,KnowledgeArticleId , UrlName,lastmodifieddate FROM Video_Article__kav WHERE (Language ='en_US' AND PublishStatus ='online' AND Topic__c = 'Step by Step' AND Sub_Topic_Step_by_Step__c =: subtopic) ORDER BY Sub_Topic_Step_by_Step__c LIMIT 10];
        Integer i = 1;
        for (Video_Article__kav newsKA: newsKAList )
        {  
            wrapperClass rec = new wrapperClass(i,newsKA);
            rec.dtlastModified = newsKA.lastmodifieddate.format('E MMM dd yyyy HH:MM:ss');
            rec.dtlastPublished = newsKA.LastPublishedDate.format('E MMM dd yyyy HH:MM:ss');
            rec.icon1 = '';
            rec.stu = newsKA;
            rec.Sr = i++;         
            lstWrapper.add(rec);
        }
        return lstWrapper;
    }
    Catch (Exception e)
    {
        return null;
    }
}

As you can see I need articles with the PublishStatus Online. I can't set it to Online while creating and like I said the kbManagement.PublishingService.publishArticle doesnt work for me.

My test Class:

@isTest 
private class NewsComponentClassTest{
  static testMethod void MakeArticle() {
    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', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='sdss123@test.com', Agent_Level__c='Level 3');

      insert u;
      Video_Article__kav newArticle = new Video_Article__kav
      (Title='test article', UrlName='testarticleurl', Language='en_US', Topic__c='Step by Step', Sub_Topic_Step_by_Step__c='Cases'  
        );


       // Insert Article
      insert newArticle;

      // Publish Article
      String articleId = newArticle.Id;
      system.debug(articleId);
      KbManagement.PublishingService.publishArticle(articleId, true);

       //create topic
       Topic newTopic = new Topic
       (Name='topicName', NetworkId='0DB24000000TNlXGAW'
        );

       insert newTopic;

       Topic newTopic2 = [select Id from Topic where Id = :newTopic.Id];

       //create topicassignment

       TopicAssignment newTopicAssignment = new TopicAssignment
       (TopicId=newTopic.Id, NetworkId='0DB24000000TNlXGAW'
        );
       System.debug('text here ' + newTopicAssignment.TopicId);

       test.startTest();


       NewsComponentClass.getNewsKAList('News', 3, 'en_US'); 
       NewsComponentClass.getNewsKAList('', 3, 'en_US');
       NewsComponentClass.getNewsKAListSearch('News', 3, 'en_US', 'search');
       NewsComponentClass.getUserLanguage();
       NewsComponentClass.getsubTopicList('subtopic');
       NewsComponentClass articleClass = new NewsComponentClass();
       articleClass.srNo = 1;
       articleClass.date1 = 'test';

       test.stopTest();

       }}

      }

Link to Publish method:
https://developer.salesforce.com/docs/atlas.en-us.knowledge_dev.meta/knowledge_dev/apex_KbManagement_PublishingService_publishArticle.htm

Does somebody have a suggestion how I can get an active Article into my test class? Hope someone can help.

Best Answer

The method KbManagement.PublishingService.publishArticle(articleId, true) needs a KnowledgeArticle Id. What you are trying to do is giving the KnowledgeArticleVersion Id to the method. You can see the difference in the suffix:

  • kav for KnowledgeArticleVersion (Video_Article__kav)
  • ka for Knowledge Article

Instead, try the following code:

String articleId = newArticle.KnowledgeArticleId;
KbManagement.PublishingService.publishArticle(articleId, true);

See the following link for more information about how the different Knowledge objects are linked to eachother.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_erd_knowledge.htm

Related Topic