- 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:56 AM
Hi @asrsharma07 ,
Update the addQuery() parameter as suggested by Sanjeev.
you can use the addQuery as below format.
knowledgeBase.addQuery('workflow_state=draft^ORworkflow_state=review');
If you are trying the publish only review state KB then remove the state filter from addQuery().
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()) {
// 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.');
Also check if any approval applied and any custom workflow running behind. Test with one record first.
-Thanks
AshishKMishra
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution