I want to get the contents of a record's attachment file

Yuto Nakayama1
Tera Contributor

Is it possible to retrieve comments written in a doc or pdf file etc. attached to a record and display them in a field?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Yuto Nakayama1 

you cannot parse doc and pdf within ServiceNow

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

View solution in original post

6 REPLIES 6

Community Alums
Not applicable

Hi @Yuto Nakayama1 ,

You can query the sys_attachment table to retrieve each record, then getContentStream and read it:

var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.orderByDesc('sys_created_on');
att.query();
while (att.next()) {
    var attach = new GlideSysAttachment().getContentStream(att.sys_id.toString());
    var reader = new GlideTextReader(attach);
    var ln = ' ';
    while ((ln = reader.readLine()) != null) {
        gs.info(ln);
    }
}

I would add another filter criteria to make sure that the MIME type starts with "text/". Otherwise there is a risk of reading a binary file line by line which is nonsense.

Community Alums
Not applicable

I agree @Maik Skoddow 

Shoheb_IbaaBoss
Tera Guru

Hi,

Please try below code.

 

(function displayCommentsFromAttachments() {
// Specify the table name and record sys_id
var tableName = 'incident'; // Replace with your table name
var recordSysId = 'your_record_sys_id'; // Replace with your record sys_id

// Query attachments related to the specified record
var attachmentGR = new GlideRecord('sys_attachment');
attachmentGR.addQuery('table_name', tableName);
attachmentGR.addQuery('table_sys_id', recordSysId);
attachmentGR.query();

// Field to display comments
var commentsField = 'comments'; // Replace with the actual field name

// Loop through the attachments
while (attachmentGR.next()) {
var attachmentSysId = attachmentGR.getUniqueValue();

// Check if the attachment is a PDF
if (attachmentGR.getValue('content_type').startsWith('application/pdf')) {
// Extract text content from the PDF
var attachmentText = GlideSysAttachment.getContent(attachmentSysId);

// Extract comments or relevant information from the text content
var extractedComments = extractCommentsFromText(attachmentText);

// Append comments to the field
if (extractedComments) {
// Retrieve existing comments from the record
var existingComments = current.getValue(commentsField) || '';
var updatedComments = existingComments + '\n' + extractedComments;

// Update the record with the combined comments
current.setValue(commentsField, updatedComments);
}
}
}

// Update the record
current.update();

// Function to extract comments from text content (customize as needed)
function extractCommentsFromText(textContent) {
// Implement logic to extract comments from text content
// For example, you might use regular expressions or other parsing techniques

// Sample logic: Extract text between "Comment:" and the next line break
var commentRegex = /Comment:(.*?)(\n|$)/g;
var match = commentRegex.exec(textContent);
return match ? match[1].trim() : '';
}
})();

 

  1. Replace 'incident' with your actual table name and 'your_record_sys_id' with the record sys_id of the specific record.
  2. Adjust the commentsField variable with the actual field name where you want to store or display the comments.
  3. The extractCommentsFromText function is a placeholder; you may need to customize this function based on the structure of your documents.

Regards,

Shoheb