Attachment Summarization with GenAI – Ready-to-Use Script

Prakash53
Tera Contributor

Hi everyone! 

If you're exploring ways to summarize attachments using ServiceNow's GenAI capabilities, here's a quick script that taps into the DocIntel GenAI plugin to process and summarize attachments linked to a specific record.

This script uses out-of-the-box Script Includes and currently supports PDF, Word, Excel, and image files.

 

 Prerequisite: Make sure the Now Assist for Platform plugin is enabled and the "Extract information from documents" skill is activated in your instance. These are required to use the summarization APIs shown below.

 

(function() {
var attachmentIds = [];
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', 'sys_id here'); // Replace with your target record Sys ID
gr.query();
while (gr.next()) {
attachmentIds.push(gr.getUniqueValue());
}

var docintelGenAIConstants = new sn_docintel_gen_ai.DocintelGenAIConstants();
var attSumAPI = new sn_docintel_gen_ai.AttachmentSummarizer();
var taskDefinitionSysId = docintelGenAIConstants.ATTACHMENT_SUMMARIZATION_AGENT_TASK_DEF_ID;

var taskID = attSumAPI.processAttachmentSysIds(attachmentIds, taskDefinitionSysId);

var docintelAPI = new sn_docintel_gen_ai.DocintelGenAIQuery();
gs.sleep(5000); // Optional: wait for summarization to complete
var result = docintelAPI.getSingleResultForTask(taskID);
gs.log('result is ' + JSON.stringify(result));
})();

 

 

  • Script Includes Used: All are OOB (DocintelGenAIConstants, AttachmentSummarizer, DocintelGenAIQuery)

  • Supported File Types: PDF, Word, Excel, and image formats

  • Customization: You can replace the table_sys_id with any record you want to summarize attachments for.

  • Async Execution: The summarization task runs asynchronously. I’ve used gs.sleep(5000) here just to simulate a delay before fetching the result. For production-grade implementations, consider using a more robust approach like polling with a scheduled job or callback mechanism to handle task completion gracefully.

 

1 REPLY 1

pradeepdasa
Tera Contributor

Hi Prakash,

 

I have a requirement where an Inbound Email Action processes incoming emails that may contain attachments, and all attachments should be summarized and a combined summary generated.

To achieve this, I implemented attachment summarization using the GenAI Attachment Summarizer / AI Agent approach. The solution works correctly for PDF and DOC/DOCX files but fails for image attachments (PNG/JPEG) and excel where it has any website links.

Below is the script I’m currently using to summarize attachments linked to the record created by the inbound email:

 

 var attachmentsysIDS = [];
    var recordID = emailSysID;
    var recordTableName = 'sys_email';
    var attachments = new GlideRecord('sys_attachment');
    attachments.addQuery('table_sys_id', recordID);
    attachments.addQuery('table_name', recordTableName);
    attachments.query();
    while (attachments.next()) {
        attachmentsysIDS.push(attachments.getUniqueValue());
    }
    if (attachmentsysIDS.length > 0) {

        var docintelGenAIConstants = new sn_docintel_gen_ai.DocintelGenAIConstants();
        var attSumAPI = new sn_docintel_gen_ai.AttachmentSummarizer();
        var taskDefinitionSysId = docintelGenAIConstants.ATTACHMENT_SUMMARIZATION_AGENT_TASK_DEF_ID;
        var taskID = attSumAPI.processAttachmentSysIds(attachmentsysIDS, taskDefinitionSysId);
        gs.sleep(3000);// for generating summary
        if (taskID) {
            var summary = new GlideRecord('sys_di_extracted_value');
            summary.addQuery('task', taskID.toString());
            summary.query();
            if (summary.next()) {
                return summary.data;
            }
        }
    }

Any guidance would be very helpful.

 

Thanks,

Prashanth.