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

GlideFather
Tera Patron

hi @fahabil698,

 

all the attachments are located at the table [sys_attachment], usually their ale linked via sys ID to particular records (incidents, ritms, etc.) - so if you have INC0001 get its sys id and search for it in the Attachment table.

Let me know if it answers your question or need more details

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */