When knowledge valid to is reached kick off the Approval Retire Workflow

daniellethomson
Tera Expert

Anyone setup a script/scheduled job to kick off the Approval Retire Workflow when the valid to date has been reached? If so, could you provide the script and/or steps involved?

Thank you in advance!

1 ACCEPTED SOLUTION

david_legrand
Kilo Sage

Hi Danielle,



It's actually pretty simple


/*


* The use case is "we would like to automatically retire the articles" (by default ServiceNow doesn't do anything with the valid_to date).


* Please find below (and attached), the script I used. The script would be a scheduled job and of course you can customize it


*


* Impact on performance: Low


* Upgradeability: No risk until ServiceNow is updating the knowledge management (a potential v4 one day)


*/




/*


* This script should be used as a Scheduled Job outside of business hours


*/


(function () {


  //Query the published and expired Knowledge articles


  var knowledge_article = new GlideRecord('kb_knowledge');


  knowledge_article.addQuery('workflow_state', 'published');


  knowledge_article.addQuery('valid_to', '<', gs.daysAgoStart(0));


  knowledge_article.query();


  while (knowledge_article.next()) {


  if (knowledge_article.kb_knowledge_base.kb_version == "3") {


  //Use the baseline API (KM v3) to retire the articles using the workflow of the Knowledge base


  new KBWorkflow().startWorkflow(knowledge_article, "retire_workflow");


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


  //Use the knowledge v2 retirement


  knowledge_article.workflow_state = 'retired';


  }


  knowledge_article.update();


  }


})();



Best regards,


View solution in original post

3 REPLIES 3

david_legrand
Kilo Sage

Hi Danielle,



It's actually pretty simple


/*


* The use case is "we would like to automatically retire the articles" (by default ServiceNow doesn't do anything with the valid_to date).


* Please find below (and attached), the script I used. The script would be a scheduled job and of course you can customize it


*


* Impact on performance: Low


* Upgradeability: No risk until ServiceNow is updating the knowledge management (a potential v4 one day)


*/




/*


* This script should be used as a Scheduled Job outside of business hours


*/


(function () {


  //Query the published and expired Knowledge articles


  var knowledge_article = new GlideRecord('kb_knowledge');


  knowledge_article.addQuery('workflow_state', 'published');


  knowledge_article.addQuery('valid_to', '<', gs.daysAgoStart(0));


  knowledge_article.query();


  while (knowledge_article.next()) {


  if (knowledge_article.kb_knowledge_base.kb_version == "3") {


  //Use the baseline API (KM v3) to retire the articles using the workflow of the Knowledge base


  new KBWorkflow().startWorkflow(knowledge_article, "retire_workflow");


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


  //Use the knowledge v2 retirement


  knowledge_article.workflow_state = 'retired';


  }


  knowledge_article.update();


  }


})();



Best regards,


Hi david where this script has to bee written ??

I have this requirement but did not work for me. Please help.