Publish knowledge article on submit

Snow Tomcal
Tera Expert

Hi, I want my knowledge article to go through the publish workflow when I press submit, and remove the publish button. My idea was to write a client script that calls a script include, but it's not working. Can anyone help please? 🙂

I also tried using a business rule also with no luck.

 

the scripts: 

client:

function onSubmit() {

var ga = new GlideAjax('workflow_knowledge');

ga.addParam('sysparm_name', 'run_workflow');

ga.addParam('sysparm_recordTable', 'kb_knowledge');

 

ga.getXMLAnswer();

}

 

server: The function that runs on the Publish UI Action.

 

var workflow_knowledge = Class.create();

workflow_knowledge.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

run_workflow: function(){

if(new KBKnowledge().canPublish(current)){

var preValue = current.workflow_state + ' ';

 

if (current.kb_knowledge_base.kb_version == "3" && new KBWorkflow().startWorkflow(current, "workflow")) {

if (current.workflow_state != 'publish';{

current.update();

}

var knowledge = new GlideRecord("kb_knowledge");   

 

if(knowledge.get(current.sys_id && preValue != knowledge.getValue("workflow_state)){

gs.addInfoMessage(new KBKnowledge().getStateMessage(knowledge.getValue("workflow_state")));

}

} else if (current.kb_knowledge_base.kb_version == "2") {

current.workflow_state = 'published';

current.update();

gs.addInfoMessage(new KBKnowledge().getStateMessage(current.getValue("workflow_state")));

}

}

11 REPLIES 11

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Snow Tomcal ,

You can create BR like below :-

GunjanKiratkar_0-1668415860854.png

 

Script :-

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    new global.KnowledgeUIAction().publish(current);
})(current, previous);

GunjanKiratkar_1-1668415891538.png

 

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi, looks really good but sadly my article still hasn't published. I can't seem to find why

Hi @Snow Tomcal ,

That code will triggered the workflow. So your article might go into the review state for approval. after approval it gets published.

 


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

I used the Instant publish workflow, but the workflow state stayed draft