Fix script to update 'valid to' date for multiple knowledge articles

astripathy
Giga Contributor

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 ?

4 REPLIES 4

Shekhar Navhak1
Kilo Sage
Kilo Sage

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!

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar

Sagar Pagar
Tera Patron

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

The world works with ServiceNow

astripathy
Giga Contributor

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.

Shekhar Navhak1
Kilo Sage
Kilo Sage

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!

Please mark the answer correct/helpful based on Impact.
Regards, Shekhar