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

AshishKM
Kilo Patron
Kilo Patron

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