- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:26 AM
I am trying to publish the records that are in the review stage using a background script. Below is the script. Please suggest. With this script, articles that are in the draft stage are getting published, but records that are in the review stage are not being published.
*Code*
var targetState = 'published';var knowledgeBase = new GlideRecord('kb_knowledge');
knowledgeBase.addQuery('workflow_state', 'IN', ['draft', 'review']);
knowledgeBase.query();
// Loop through the articles and publish item
while (knowledgeBase.next()) {
// Check if the article is in the "Review" stage
if (knowledgeBase.getValue('workflow_state') === 'review') {
// Set the target state
knowledgeBase.setValue('workflow_state', targetState);
// Update the record
knowledgeBase.update();
gs.info('Published Knowledge Article: ' + knowledgeBase.getValue('number'));
} else {
gs.info('Skipping article in non-Review state: ' + knowledgeBase.getValue('number'));
}
}
gs.info('Script execution completed.');
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:46 AM
If this is your entire script and purpose, aside from the errant [] that is causing the addQuery line to be ignored, resulting in all records being returned, I'm not sure why you're querying for records in 2 states, but then only taking action on one. This script is not directly updating records with the workflow_state = 'draft', so if you're seeing that change after running the script it's likely a Business Rule that runs before/after update, which for testing purposes you should disable:
var targetState = 'published';
var knowledgeBase = new GlideRecord('kb_knowledge');
knowledgeBase.addQuery('workflow_state', 'review');
knowledgeBase.query();
// Loop through the articles and publish item
while (knowledgeBase.next()) {
// Set the target state
knowledgeBase.setValue('workflow_state', targetState);
//disable Business Rules from running
knowledgeBase.setWorkflow(false);
// Update the record
knowledgeBase.update();
gs.info('Published Knowledge Article: ' + knowledgeBase.getValue('number'));
}
gs.info('Script execution completed.');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:35 AM
Can you update your query to below and try?
knowledgeBase.addQuery('workflow_state', 'IN', 'draft,review');
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:39 AM
Another option is to try this script
var answer=new KBWorkflow().progressStatus(knowledgeBase,targetState);
knowledgeBase.update();
var context=new KBWorkflow().startWorkflow(knowledgeBase,'workflow');
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:39 AM
Still it's not working, even after updating the above line of code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:46 AM
If this is your entire script and purpose, aside from the errant [] that is causing the addQuery line to be ignored, resulting in all records being returned, I'm not sure why you're querying for records in 2 states, but then only taking action on one. This script is not directly updating records with the workflow_state = 'draft', so if you're seeing that change after running the script it's likely a Business Rule that runs before/after update, which for testing purposes you should disable:
var targetState = 'published';
var knowledgeBase = new GlideRecord('kb_knowledge');
knowledgeBase.addQuery('workflow_state', 'review');
knowledgeBase.query();
// Loop through the articles and publish item
while (knowledgeBase.next()) {
// Set the target state
knowledgeBase.setValue('workflow_state', targetState);
//disable Business Rules from running
knowledgeBase.setWorkflow(false);
// Update the record
knowledgeBase.update();
gs.info('Published Knowledge Article: ' + knowledgeBase.getValue('number'));
}
gs.info('Script execution completed.');