Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fix script to bulk update category field of published knowledge article.

SM123
Tera Expert

Hello,

URGENT

I require a fix script to bulk update category field of the published knowledge article. Yes, I'm aware of (glide.knowman.versioning.enable_minor_edits) this system property but our organization is not allowing us to enable it so i require a fix script as alternative. 

I gathered this fix script but it not working as expected :

Could anyone please help.

var kb = new GlideRecord("kb_knowledge");
kb.addEncodedQuery("workflow_state=published^short_description=test"); // Change the encoded query to target published articles with a specific short description if needed
kb.query();
while (kb.next()) {
    kb.category = "Suppliers"; // Set the category field to "Email"
    kb.update();
}

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @SM123 ,

I tried your problem in my PDI and it works for me please make some changes in your script like there is no field called categeroy instead of kb_category and you have to pass the sys_id of emial category as I did in below scirpt. This is working in my PDI.

var kb = new GlideRecord("kb_knowledge");
kb.addEncodedQuery("workflow_state=published^short_description=test"); // Change the encoded query to target published articles with a specific short description if needed
kb.query();
while (kb.next()) {
	gs.print("Herhe")
    kb.kb_category = "5681bf8bff0221009b20ffffffffff95"; // Set the category field to "Email" Set sysid of email here
    kb.update();
}

 

SarthakKashyap_0-1716406037849.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

View solution in original post

3 REPLIES 3

KevinBellardine
Kilo Sage

I just ran this in my PDI and was able to make updates, what isn't working for you?

Dom Gattuso
Mega Sage

@SM123 - it looks like you are setting 'category' instead of 'kb_category'. Also the category value should be set to a sys_id rather than a name value. You may be running into a business rule that blocks edits to a published article which can be fixed by using setworkflow(false) in you script. I have provided an example below:

 

 

var new_category_name = "Suppliers";

var category_id = new GlideRecord('kb_category');
if(category_id.get('label', new_category_name)){

    var kb = new GlideRecord("kb_knowledge");
    kb.addEncodedQuery("workflow_state=published^short_description=test"); // Change the encoded query to target published articles with a specific short description if needed
    kb.query();
    while (kb.next()) {
        kb.setValue('kb_category', category_id); 
        kb.setWorkflow(false); // Prevent workflow and Business rules from running on update
        kb.update();
    }
}

 

Community Alums
Not applicable

Hi @SM123 ,

I tried your problem in my PDI and it works for me please make some changes in your script like there is no field called categeroy instead of kb_category and you have to pass the sys_id of emial category as I did in below scirpt. This is working in my PDI.

var kb = new GlideRecord("kb_knowledge");
kb.addEncodedQuery("workflow_state=published^short_description=test"); // Change the encoded query to target published articles with a specific short description if needed
kb.query();
while (kb.next()) {
	gs.print("Herhe")
    kb.kb_category = "5681bf8bff0221009b20ffffffffff95"; // Set the category field to "Email" Set sysid of email here
    kb.update();
}

 

SarthakKashyap_0-1716406037849.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak