+ Start a Discussion
noedskovnoedskov 

How to create a knowledgebase__kav with PublishStatus = Online for testing?

Hi,

 

I have a test method which tests a number of Knowledgebase related objects. In my Apex Class, I have the following code:

 

 

        String queryvar = 'FIND \'' + '*' + searchQ + '*' + '\'' + ' IN ALL FIELDS Returning ' + 'Knowledgebase__kav(Id, Title, Summary, UrlName WHERE PublishStatus =\'Online\')';
        List<List<SObject>> searchList = search.query(queryvar);
        Knowledgebase__kav [] documents = ((List<Knowledgebase__kav>)searchList[0]);
        if (!documents.isEmpty()) {
            for (Knowledgebase__kav c : documents) {
                // Do stuff...
            }   
        }  else {
            // Do different stuff...
        }

In my test method I then create a new Knowlegebase__kav object but when trying to set PublishStatus = 'Online', I'm told the field is not writeable. I need to mark it as 'Online' as that's what my Apex Class is looking for.

 

 

Any great ideas?

 

Cheers.

 

/Søren Nødskov Hansen

Best Answer chosen by Admin (Salesforce Developers) 
FrancoisLptxFrancoisLptx

Hi,

 

Unfortunately, it's not possible to create Online Article with the API today.

 

As workaround, I think that you can update your code to take a parameter 'Draft'/'Online'. Your real code can use the default value Online and your test class can run the code using Draft mode.

All Answers

noedskovnoedskov

Just a quick update...

 

I tried, in my test method, to select a user who has the "Knowledge User" flag set to true and encapsulate the line of code where I set the PublishStatus using runAs...

 

Something like this:

		// Locate Knowledge User
		User user = [SELECT Id FROM User WHERE Id = ''];

		// Create Knowledgebase__kav
		Knowledgebase__kav kb = new Knowledgebase__kav();
		kb.Title = 'This is a KB'; // We're looking for '*a*'
		kb.Summary = 'KB Summary';
		kb.UrlName = 'KB-UrlName';
		System.runAs(user) {
			kb.PublishStatus = 'Online';
		}
		insert kb;

 

 

However, this didn't do the trick either. I really hope someone out there knows how to solve this problem.

 

If you know (or think you know) what I'm doing wrong, please let me know. It's getting very frustrating and this is the only part preventing us from putting our code into production.

 

Thanks.

 

/Søren Nødskov Hansen

FrancoisLptxFrancoisLptx

Hi,

 

Unfortunately, it's not possible to create Online Article with the API today.

 

As workaround, I think that you can update your code to take a parameter 'Draft'/'Online'. Your real code can use the default value Online and your test class can run the code using Draft mode.

This was selected as the best answer
noedskovnoedskov

Thanks for pointing out the obvious, FrancoisLptx!

Philip BologniniPhilip Bolognini
You can use the KbManagement.PublishingService class:
KbManagement.PublishingService.publishArticle(kb.Id, true);


Documentation
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_knowledge_kbManagement.htm