Update multiple articles by script

Daniel Vieira
Tera Expert

Hi everyone,

 

I need to update a large number of articles in ServiceNow. The goal is to keep each article as the previous one but with a new version. This is necessary because I have a Business Rule that should run upon update to make some adjustments to the articles.

 

Since this only needs to happen once, I was thinking of using a Scheduled Job. However, the script I’ve been using ends up creating a new article with the same number, but it doesn’t maintain the relationship with the previous version. What I actually want is to create a new version of the existing article and publish it—just like we can do manually through the UI.

 

Doing this manually is not ideal due to the volume of articles. Is there a script or method that can help me update all articles efficiently while preserving their version history and triggering the Business Rule?

 

Thanks in advance!

Daniel Vieira

6 REPLIES 6

Harshal Aditya
Mega Sage
Mega Sage

Hi @Daniel Vieira - Good Day

Could you please share the script that you have tried ?

Something like that

 

var gr = new GlideRecord('kb_knowledge');
if (gr.get('#sys_id')) {

// Create a new version by copying the current record
var newVersion = new GlideRecord('kb_knowledge');
newVersion.initialize();

// Copy relevant fields from the original
newVersion.short_description = gr.short_description;
newVersion.kb_category = gr.kb_category;
newVersion.kb_knowledge_base = gr.kb_knowledge_base;
newVersion.name = gr.name;
newVersion.parent = gr.sys_id; // Link to original article
newVersion.language = gr.language;
newVersion.sys_class_name = gr.sys_class_name;

// Set new content
newVersion.text = 'This is the new article body with updated content.';

// Publish the new version
newVersion.workflow_state = 'published';
newVersion.published = true;

// Insert the new version
newVersion.insert();

gs.info('New version created and published for article: ' + gr.getDisplayValue('number'));
} else {
gs.info('Article not found.');
}

Ankur Bawiskar
Tera Patron
Tera Patron

@Daniel Vieira 

you can use KnowledgeManagement API for this.

Knowledge Management REST API 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi,

 

How can I use that? I'm asking you because I just saw get methods to retrieve articles. I need for example, a post, to update articles.

 

Thanks,

Daniel Vieira