Publish the knowledge articles which are in Review Stage using background script

asrsharma07
Tera Contributor

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

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

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.');

  

View solution in original post

5 REPLIES 5

SanjivMeher
Kilo Patron
Kilo Patron

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.

SanjivMeher
Kilo Patron
Kilo Patron

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.

asrsharma07
Tera Contributor

Still it's not working, even after updating the above line of code.

Brad Bowman
Kilo Patron
Kilo Patron

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.');