- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 10:24 PM
Is it possible to retrieve comments written in a doc or pdf file etc. attached to a record and display them in a field?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:06 AM
you cannot parse doc and pdf within ServiceNow
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2024 11:22 PM
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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 02:11 AM
I agree @Maik Skoddow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2024 12:02 AM
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() : '';
}
})();
- Replace 'incident' with your actual table name and 'your_record_sys_id' with the record sys_id of the specific record.
- Adjust the commentsField variable with the actual field name where you want to store or display the comments.
- The extractCommentsFromText function is a placeholder; you may need to customize this function based on the structure of your documents.
Regards,
Shoheb