Can not Read attachment files (txt , json, doc) in KB article

deepaktomar
Tera Contributor

Hi Everyone,

I’m working on a use case where I need to read the content of an attachment file (like a .txt or .json file) that is attached to a Knowledge Article.

 

I tried the following script, file name is coming correctly but the file content is empty. Can someone please guide me on the correct way to read the content of the attached file?

 

Here is the code I’ve used so far:



var attachments = [];
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.addQuery('table_sys_id', '<sys_id of kb article');
attachmentGR.query();

while (attachmentGR.next()) {
    var fileName = attachmentGR.file_name.toString();
    var sa = new GlideSysAttachment();
    var stream = sa.getContentStream(attachmentGR);
   
    if (stream && (fileName.endsWith('.txt') || fileName.endsWith('.json') || fileName.endsWith('.csv'))) {
        var reader = new GlideTextReader(stream);
        var content = '';
        var line;
        while ((line = reader.readLine()) !== null) {
            content += line + '\n';
        }

        attachments.push({
            FileName: fileName,
            FileContent: content
        });
    }
}
gs.info('JSON result' + JSON.stringify(attachments));

JSON result  [   {"FileName":    "cmdb_ci_data.txt",      "FileContent":""}]



Regards
Deepak Tomar

3 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@deepaktomar 

getContentStream() required attachment sysId, but you are passing the GlideRecord object

I updated below, can you try this?

AnkurBawiskar_0-1747231707984.png

 

var attachments = [];
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.addQuery('table_sys_id', '<sys_id of kb article>'); // Replace with actual sys_id
attachmentGR.query();

while (attachmentGR.next()) {
    var fileName = attachmentGR.file_name.toString();
    // Pass the sys_id string, not the GlideRecord, to getContentStream()
    var sa = new GlideSysAttachment();
    var stream = sa.getContentStream(attachmentGR.sys_id.toString());

    if (stream && (fileName.endsWith('.txt') || fileName.endsWith('.json') || fileName.endsWith('.csv'))) {
        var reader = new GlideTextReader(stream);
        var content = '';
        var line;
        while ((line = reader.readLine()) !== null) {
            content += line + '\n';
        }

        attachments.push({
            FileName: fileName,
            FileContent: content
        });
    }
}
gs.info('JSON result: ' + JSON.stringify(attachments));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@deepaktomar 

Glad to know.

You cannot print doc, docx, pdf file etc as those are not in plain text and not supported by ServiceNow to read the content.

You can only read txt, csv files etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@deepaktomar 

getContentStream() required attachment sysId, but you are passing the GlideRecord object

I updated below, can you try this?

AnkurBawiskar_0-1747231707984.png

 

var attachments = [];
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'kb_knowledge');
attachmentGR.addQuery('table_sys_id', '<sys_id of kb article>'); // Replace with actual sys_id
attachmentGR.query();

while (attachmentGR.next()) {
    var fileName = attachmentGR.file_name.toString();
    // Pass the sys_id string, not the GlideRecord, to getContentStream()
    var sa = new GlideSysAttachment();
    var stream = sa.getContentStream(attachmentGR.sys_id.toString());

    if (stream && (fileName.endsWith('.txt') || fileName.endsWith('.json') || fileName.endsWith('.csv'))) {
        var reader = new GlideTextReader(stream);
        var content = '';
        var line;
        while ((line = reader.readLine()) !== null) {
            content += line + '\n';
        }

        attachments.push({
            FileName: fileName,
            FileContent: content
        });
    }
}
gs.info('JSON result: ' + JSON.stringify(attachments));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar ,

It is working fine. Do I need to change the script to read the .docx file because I am getting the binary data with the current script.

Regards
Deepak Tomar

@deepaktomar 

Glad to know.

You cannot print doc, docx, pdf file etc as those are not in plain text and not supported by ServiceNow to read the content.

You can only read txt, csv files etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@deepaktomar 

Glad to know that my script worked.

Please mark my response as correct and close the thread.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader