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.
Best Answer
Try to pass
KnowledgeArticleId
of your Articleknowledge__kav
. You might need to again retrieve the article record you have inserted and then PassKnowledgeArticleId
. In your case it will be :