- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 12:15 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 02:28 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 01:54 AM
Hi,
By read, do you mean, you have to do ETL the data from those attached files?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 02:19 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 02:28 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2025 02:34 AM
Thanks! Works great!