3

I am trying to publish an article in my test code yet even with the exact code copied from the salesforce documentation nothing will allow me to publish an article. I have been working on this for days now and am always getting stuck on this step. Everything works if I allow it to run on test articles yet that isn't what I want.

Main Class (I want to be able to publish the knowledge articles so the second for loop which includes draft articles isn't used.)

    public class KnowledgeTopicUpdate {
    @InvocableMethod(label='Update the knowledge topics' description='Runs when you update a knowledge article and sets the topic based off of the Team.')
    public static void UpdateTopic(List<ID> Ids){
        List<TopicAssignment> TopicIDs = [select TopicId, NetworkID from topicassignment where EntityId = :Ids];
        List<Knowledge__kav> Knowledge1 = [select Id from knowledge__kav where team__c = :Ids and language = 'en_US' and publishstatus = 'online'];
        List<Knowledge__kav> Knowledge2 = [select Id from knowledge__kav where team__c = :Ids and language = 'en_US' and publishstatus = 'draft'];
            for(TopicAssignment TopicAssignment:TopicIDs){
                for(Knowledge__kav TotalKnowledge: Knowledge1){
                    TopicAssignment a = new TopicAssignment(TopicId=TopicAssignment.TopicId, EntityId=TotalKnowledge.Id, NetworkId=TopicAssignment.NetworkID);
                    insert a;
                }
            }
            for(TopicAssignment TopicAssignment:TopicIDs){
                for(Knowledge__kav TotalKnowledge: Knowledge2){
                    TopicAssignment a = new TopicAssignment(TopicId=TopicAssignment.TopicId, EntityId=TotalKnowledge.Id, NetworkId=TopicAssignment.NetworkID);
                    insert a;
                }
            }
    }
}

Test Class

@isTest(seeAllData=false)
public class KnowledgeTopicUpdate_UT {
//need a knowledge article and a team and a topic assignment and need to create a topic assignment for the knowledge article based off of the team.
  static testMethod void MakeArticle() {
      contact newContact = new contact(RecordTypeId = '0120Y0000008qLvQAI', LastName = 'test', AccountID = '0010Y0000054qWDQAY');
      insert newContact;
      String contactId = newContact.Id;
      team__c newTeam = new team__c(Name = 'testTeam', Team_Manager__c = contactId);
      insert newTeam;
      String teamID = newTeam.Id;
      knowledge__kav newArticle = new knowledge__kav(Title='test',summary='xyz',urlname='xyz', team__c = teamID);
      topic newTopic = new topic(NetworkId = '0DB0Y000000CablWAC', name = 'test');
      insert newTopic;
      string topicID = newTopic.Id;
      insert newArticle;
      String articleId = newArticle.Id;
      TopicAssignment Topicass = new TopicAssignment(TopicId = topicID, EntityId = teamID, NetworkId = '0DB0Y000000CablWAC');
      insert Topicass;
      boolean archived = false;
      List<Id> Id = new List<Id>(new Map<Id, team__c>([select Id from team__c where id = :teamID limit 1]).keySet());
      KbManagement.PublishingService.publishArticle(articleId, true);
      test.startTest();
      KnowledgeTopicUpdate.UpdateTopic(Id);   
      if([select count() from topicassignment where entityid = :articleId] == 1){
          archived = true;
      }
      system.assertEquals(true, archived);
      test.stopTest();
  }
}

Error

System.InvalidParameterValueException: Invalid ID.
Class.KbManagement.PublishingService.publishArticle: line 5, column 1
Class.KnowledgeTopicUpdate_UT.MakeArticle: line 21, column 1

Thanks in advance.

3 Answers 3

5

Try to pass KnowledgeArticleId of your Article knowledge__kav. You might need to again retrieve the article record you have inserted and then Pass KnowledgeArticleId. In your case it will be :

knowledge__kav obj1 = [SELECT Id,Title,KnowledgeArticleId FROM knowledge__kav WHERE id =: newArticle.Id];


KbManagement.PublishingService.publishArticle(obj1.KnowledgeArticleId, true);
0
2

I created a code snippet for future use.

More details here.

Code contains the following methods:

  • getAllArticles
  • createNewArticleAsADraft
  • publishArticle
  • unPublishArticle
  • updateDraftArticleWithoutPublish
  • updatetArticle

Here you can find a code example:

public with sharing class ArticlesUtils {

@AuraEnabled
public static List<Knowledge__kav> getAllArticles(){
    return [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav ];
}

@AuraEnabled
public static String createNewArticleAsADraft(String title, String urlName) {

    Knowledge__kav newArticle = new Knowledge__kav();
    newArticle.Title = title;
    newArticle.UrlName = urlName;
    insert newArticle;

    return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticle.Id].KnowledgeArticleId;
}

@AuraEnabled
public static void publishArticle(String recordId) { //It need to be KnowledgeArticleId
    KbManagement.PublishingService.publishArticle(recordId, true);
}

@AuraEnabled
public static String unPublishArticle(String recordId){ //It need to be KnowledgeArticleId
    String newArticleId = KbManagement.PublishingService.editOnlineArticle(recordId, true); 
    return [SELECT KnowledgeArticleId FROM Knowledge__kav WHERE Id =: newArticleId].KnowledgeArticleId;
}

@AuraEnabled
public static String updateDraftArticleWithoutPublish(String title, String urlName, Id recordId) {

    Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: recordId ];   

    newArticle.Title = title;
    newArticle.UrlName = urlName;

    update newArticle;

    return newArticle.KnowledgeArticleId;
}


@AuraEnabled
public static String updatetArticle(String title, String urlName, Id recordId) {

    String newVersionId = unPublishArticle(recordId);

    Knowledge__kav newArticle = [ SELECT Id, KnowledgeArticleId, Title, UrlName FROM Knowledge__kav WHERE KnowledgeArticleId =: newVersionId ];   

    newArticle.Title = title;
    newArticle.UrlName = urlName;

    update newArticle;

    publishArticle(newVersionId);

    return newVersionId;
}
}

Example of usage:

List<Knowledge__kav> articles = ArticlesUtils.getAllArticles();
String newArticleKnowledgeId = ArticlesUtils.createNewArticleAsADraft('SalesforceProfs', 'salesforce-profs');
ArticlesUtils.publishArticle(newArticleKnowledgeId);
//unpublish, update, publish > separate actions
String newArticleVersionId = ArticlesUtils.unPublishArticle(newArticleKnowledgeId);
ArticlesUtils.updateDraftArticleWithoutPublish('SalesforceProfs Update', 'salesforce-profs-update', newArticleVersionId);
ArticlesUtils.publishArticle(newArticleVersionId);
//update - contain unpublish, update, publish
//ArticlesUtils.updatetArticle('SalesforceProfs Update', 'salesforce-profs-update', newArticleKnowledgeId)
0

After publishing the article using the below code .Query the article again to view the 'Online' publishStatus

knowledge__kav obj1 = [SELECT Id,Title,KnowledgeArticleId FROM knowledge__kav WHERE id =: newArticle.Id];
KbManagement.PublishingService.publishArticle(obj1.KnowledgeArticleId, true);

knowledge__kav obj2 = [SELECT Id,Title,KnowledgeArticleId FROM knowledge__kav WHERE id =: newArticle.Id];

You must log in to answer this question.

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