How to remove related articles records from related list in Knowledge article via fix script

Patiha
Kilo Contributor

Hi Community,

I have a requirement where all the records that are present in Related Articles(related list) in Knowledge article record to be removed from the related list but should not be deleted from the table, Only remove from related list via a script. I want to achieve this via a fix script. Could someone help me on this.

Thanks in advance!

1 ACCEPTED SOLUTION

Martin Ivanov
Giga Sage
Giga Sage

Hi. You need to run this code in bg script/fix script

var current = 'd7dc75b5474321009db4b5b08b9a7150'; //Replace with sys_id of the Main record, from which you want to remove the relared articles

var grRelated = new GlideRecord('kb_2_kb');
grRelated.addQuery('kb_knowledge', current);
grRelated.query();

grRelated.deleteMultiple();

This will not delete the related articles from the kb_knowledge table, but will delete only the relationships!

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

View solution in original post

7 REPLIES 7

Martin Ivanov
Giga Sage
Giga Sage

Hi. You need to run this code in bg script/fix script

var current = 'd7dc75b5474321009db4b5b08b9a7150'; //Replace with sys_id of the Main record, from which you want to remove the relared articles

var grRelated = new GlideRecord('kb_2_kb');
grRelated.addQuery('kb_knowledge', current);
grRelated.query();

grRelated.deleteMultiple();

This will not delete the related articles from the kb_knowledge table, but will delete only the relationships!

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Hi @Martin Ivanov 

Thanks for your response. 

I want to remove the related articles for 1000's of knowledge articles. As the above code shows how to remove them for each record with sys_id. So how do we achieve it for multiple articles.

Thanks!!

Then go to the kb_knowledge table, build your query and copy it (hope you know how to copy encoded query, let me know if you don't).

Then your script for the fix script will be

var encodedQuery = 'active=true^descriptionLIKEsomething'; //replace with yours

var grKBArticle = new GlideRecord('kb_knowledge');
grKBArticle.addEncodedQuery(encodedQuery);
grKBArticle.query();

while(grKBArticle.next()){


var grRelated = new GlideRecord('kb_2_kb');
grRelated.addQuery('kb_knowledge', grKBArticle.getUniqueValue());
grRelated.query();

grRelated.deleteMultiple();

}

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022

 


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Hi @Martin Ivanov ,

Thanks for the answer. That helped me achieve the solution. I also want this part where i want to remove only the related articles whose state is retired and not other related articles which are published. How do we achieve this?

Thanks!