Fix script to update 'valid to' date for multiple knowledge articles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2022 03:06 AM
Hi,
We have the requirement to extend the 'valid to' date for some 4000 KB articles till 1 year from now. Can anyone share ideas on how to do it via a fix script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2022 03:12 AM - edited ‎12-14-2022 03:14 AM
Hi
You can use a script like this to bulk change valid_to date for KB articles, this script will get you started:
var gr = new GlideRecord('kb_knowledge');
//gr.addQuery('workflow_state', 'published'); add the condition for 4000 article
gr.query();
while(gr.next()){
gr.valid_to = 'YYYY-MM-DD';
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
}
Just pick a date using that format of YYYY-MM-DD, so for example: 2022-05-01.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Regards, Shekhar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2022 03:14 AM
Hi @astripathy,
Try this sample scritps -
var query = "copied_filter_query";
var kb = new GlideRecord("kb_knowledge");
kb.addEncodedQuery(query); // add filter query
kb.query();
while(kb.next()){
kb.valid_date = "add_date_here";
kb.setWorkflow(false);
kb.autoSysFields(false);
kb.update();
}
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2022 03:34 AM
Thanks for the replies everyone. The code works, but is there a way to set the valid to as 1 year from now(dynamically). As in 1 year from when the code is being run so that the fix script can be used again in future.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-14-2022 04:08 AM - edited ‎12-14-2022 04:10 AM
Hi
instead of adding static date, update code with below script it will take next year date:
var gr = new GlideRecord('kb_knowledge');
//gr.addQuery('workflow_state', 'published'); add the condition for 4000 article
gr.query();
while(gr.next()){
gr.valid_to = new GlideDateTime(gs.nowDateTime());gdt.addYears(1);gdt.getDate();
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
}
Please mark reply as Helpful/Correct, if applicable. Thanks!
Regards, Shekhar