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.

Generate KB in .txt file

Ariomar de Deus
Tera Contributor

I need to generate my KB in a .txt file, but there is no such option in ServiceNow. Is it possible to implement a script to perform this generation?

1 REPLY 1

Vishal Jaswal
Giga Sage
Giga Sage

Hello @Ariomar de Deus 

var kbSysId = '66c72e39c3012210587bf3ddd40131c0'; // Replace with your KB article sys_id
var grKB = new GlideRecord('kb_knowledge');
if (grKB.get(kbSysId)) {
	
 // Ensure it's a string and remove HTML tags using regex (I use regex generators online)
   var rawHtml = grKB.text + '';
   var plainText = rawHtml.replace(/<[^>]*>/g, '');
   var fileName = grKB.number + '.txt'; // you can use any naming convention
   
   // Attach the .txt file to the KB article or you can attach it to the sys_attachment table as well
   var attachment = new Attachment();
   attachment.write('kb_knowledge', kbSysId, fileName, 'text/plain', plainText);  //syntax of attachment.write is leveraged from https://www.servicenow.com/docs/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/GlideSysAttachmentGlobal/concept/GlideSysAttachmentGlobalAPI.html

Hope that helps!