Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Automatically update "Valid To" of previous article version when a new draft is created

vivek11
Tera Contributor

How to update "Valid To" date of the previous published version automatically when a new draft is created
to make sure previous version does not go to pending retirement avoiding multiple published versions or accidental retirement of the latest version

1 REPLY 1

vaishali231
Kilo Sage

Hey @vivek11 

You can handle this requirement by updating the previous published version at the time a new draft is created, instead of relying on retirement workflows.

Approach (Business Rule)

Create a Before Insert Business Rule on kb_knowledge:

Condition:

  • workflow_state = draft

Script:

(function executeRule(current, previous) {
   if (current.workflow_state != 'draft')
       return;
   var kb = new GlideRecord('kb_knowledge');
   kb.addQuery('number', current.number);
   kb.addQuery('workflow_state', 'published');
   kb.addQuery('sys_id', '!=', current.sys_id);
   kb.orderByDesc('version');
   kb.query();
   if (kb.next()) {
       kb.valid_to = new GlideDateTime(); // expire previous version
       kb.update();
   }
})(current, previous);

Why this works

Ensures only one active published version

Prevents previous version from going into Pending Retirement

Avoids accidental retirement of the latest version

Uses Valid To (recommended lifecycle control instead of forcing state change)

 

Notes

  1. Do not set previous version to Retired (this triggers workflows and can cause issues)
  2. number links all versions of the same article
  3. This approach keeps older versions for history but makes them inactive

*********************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb