5

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='[email protected]', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = p.Id, 
        TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]', 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.

4
  • 1
    I have several test classes using it, the first obvious difference is I am using: KbManagement.PublishingService.publishArticle(newArticle.Id, true); Does this work for you?
    – Girbot
    Commented May 31, 2016 at 12:05
  • Since you're not using SeeAllData, it would seem that the topic you're assigning when you insert the KbArticle should also be created first. At a minimum, check to see if it exists in your SB.
    – crmprogdev
    Commented May 31, 2016 at 12:54
  • Girbot, this sadly did not change anything for me. It still gave the "System.InvalidParameterValueException: Invalid ID." error
    – Jan
    Commented May 31, 2016 at 13:56
  • crmprogdev, why should there be a topic already? publishing is the step where you expose the Article to a certain Community right? Once its there, you assign a Topic. Or am I completely wrong? I think the issue is with inserting an Article with PublishStatus='Online'.
    – Jan
    Commented May 31, 2016 at 13:59

1 Answer 1

5

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .