Locate the attachment of a record and read its content

fahabil698
Mega Contributor

Hello SN community. 
I have a batch of records that have attached a file that comes from another integration. I have a task where I require to locate the attached file and read it. Can you please provide with some guidance on how I can do that?

1 ACCEPTED SOLUTION

iekosmadakis
Mega Sage

Hello @fahabil698 !

Assuming the attachments are of type .json, .txt, etc., here is a sample script you can use and adapt to your needs to read the contents of the attachment:

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', '<your_table_name>');
grAttachment.addQuery('table_sys_id', '<your_record_sys_id>');
grAttachment.query();
if (grAttachment.next()) {
    var inputStream = new GlideSysAttachment().getContentStream(grAttachment.getUniqueValue());
    if (inputStream) {
        var reader = new GlideTextReader(inputStream);
        var line = '';
        var fileContent = '';
        while ((line = reader.readLine()) !== null) {
            fileContent += line;
        }
        gs.info(fileContent);
    }
}

 

Please consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.

View solution in original post

5 REPLIES 5

Manoj89
Giga Sage

Hi,

 

By read, do you mean, you have to do ETL the data from those attached files?

SaiHarshaS
Mega Contributor

Hello @fahabil698,

Firstly 

Locate the Attachment Use the sys_attachment table to find the file.

var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', 'your_table_name'); // e.g., 'incident'
attachmentGR.addQuery('table_sys_id', 'your_record_sys_id');
attachmentGR.query();
if (attachmentGR.next()) {
var attachmentSysId = attachmentGR.getValue('sys_id');
}

Read Attachment Content Use the GlideSysAttachment API

var gsa = new GlideSysAttachment();
var fileContent = gsa.getContentStream(attachmentGR);
var reader = new GlideTextReader(fileContent);

var text = '';
var line;
while ((line = reader.readLine()) != null) {
text += line + '\n';
}
gs.info(text); // Or process the content as needed

iekosmadakis
Mega Sage

Hello @fahabil698 !

Assuming the attachments are of type .json, .txt, etc., here is a sample script you can use and adapt to your needs to read the contents of the attachment:

var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', '<your_table_name>');
grAttachment.addQuery('table_sys_id', '<your_record_sys_id>');
grAttachment.query();
if (grAttachment.next()) {
    var inputStream = new GlideSysAttachment().getContentStream(grAttachment.getUniqueValue());
    if (inputStream) {
        var reader = new GlideTextReader(inputStream);
        var line = '';
        var fileContent = '';
        while ((line = reader.readLine()) !== null) {
            fileContent += line;
        }
        gs.info(fileContent);
    }
}

 

Please consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.

Thanks! Works great!