The CreatorCon Call for Content is officially open! Get started here.

add attachments to the table through email

averyscotts
Kilo Contributor

Hi

How to attach any files to the incident record through inbound email action.
In the instance there is a OOTB create incident inbound email action but there is no any attachment related code over there.

3 REPLIES 3

Ivan Betev
Mega Sage
Mega Sage

Deepak Shaerma
Kilo Sage
Kilo Sage

Hi @averyscotts 

1. Attachment Handling: ServiceNow automatically saves email attachments to the sys_attachment ttable and links them to the inbound email record. Your task is to write a script within your inbound email action to re-link these attachments from the email record to the appropriate incident record.


Below is a sample script you might add to your inbound email action to handle attachments. This assumes you’re working within the existing OOTB create incident action or a similar custom one designed for handling incoming emails intended to create or update incidents.

(function runAction(email, email_action, event) {
    // Assuming ‘incident’ variable is your Incident GR from the earlier part of email action
    var incidentGR = new GlideRecord('incident');
    if (incidentGR.get('number', email.subject.trim())) { // Custom logic to find incident based on email subject or some other criteria
        // Now handling attachments
        var attachment = new GlideSysAttachment();
        var emailAttachments = new GlideRecord('sys_attachment');
        emailAttachments.addQuery('table_sys_id', email.sys_id); // ‘email.sys_id’ refers to the current inbound email record
        emailAttachments.query();
        while (emailAttachments.next()) {
            // Move attachments from email record to the incident record
            attachment.copy(emailAttachments.getTableName(), emailAttachments.getUniqueValue(), incidentGR.getTableName(), incidentGR.getUniqueValue());
        }
    } else {
        // Your code to handle creating a new incident if not found
    }
})(email, email_action, event);

The sample script provided is a base template. You may need to adjust the logic for finding the correct incident record (e.g., using the email subject, body, or other criteria).
Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

 

Sumanth16
Kilo Patron

Hi @averyscotts ,

 

  1. Below is a simplified example of how you might modify the script to handle attachments. This example assumes that the attachments are included as part of the email body

 

// Extract attachments from email
var attachments = email.getAttachments();

// Iterate over attachments
for (var i = 0; i < attachments.size(); i++) {
    var attachment = attachments.get(i);
    
    // Create attachment record
    var attachmentRecord = new GlideRecord('sys_attachment');
    attachmentRecord.initialize();
    attachmentRecord.table_name = 'incident';
    attachmentRecord.file_name = attachment.getName();
    attachmentRecord.content_type = attachment.getContentType();
    attachmentRecord.file_size = attachment.getSize();
    attachmentRecord.sys_created_by = gs.getUserID(); // Set the creator as needed
    attachmentRecord.insert();

    // Associate attachment with incident record
    var incidentAttachment = new GlideSysAttachment();
    incidentAttachment.write(incidentSysID, attachmentRecord.getUniqueValue());
}

 

(or)

Second approach:

Use flow designer to attach email attachments to incident:

https://www.youtube.com/watch?v=dVAEKCql4q4

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

The Copy Attachment action has a bug where it accepts records from any table but only records from sys_attachment will work. You get the error The Record type sc_req_item does not match with Attachment table type when running the flow. This video looks at how to copy attachments from an inbound ...