How to add received i email as an attachment in notification?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-09-2023 01:28 AM
Hello,
I have a requirement, I want to add an email which is received into ServiceNow as an attachment in the notification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-09-2023 04:52 AM
Hi @Thota Naga Jyo1 ,
To fulfill your requirement, you need to create an Inbound Email Action:
1. Navigate to Inbound Actions
2. Create New
3. Put relevant conditions in When to Run to identify your email (such as "Subject" starts with "xyz", Recipients is "abc", etc.)
4. In Actions write the script on target table to fetch the record where you want to add the attachment and add below line of code to your script:
var str_gr = new GlideRecord("your_table_name");
str_gr.addEncodedQuery("condition");
str_gr.query();
if (str_gr.next()) {
/* Attaches the incoming email as an attachment to the current record */
var attachment = new GlideSysAttachment();
fileName = email.subject + '_email.eml'; // Name of the attachment
contentType = 'application/octet-stream'; // Type of attachment
content = email.content_type + "\n" + email.headers + "\n\n\n\n" + email.body_text; // Extracts content of email into attachment
str_gr.update(); // Updates the current record
attachment.write(str_gr, fileName, contentType, content); // Adds attachment to the record
}
5. Save the record.
Thus, this Inbound action will trigger once an email is received in ServiceNow and matches your set of conditions.
And the email will get attached on the specific record on target table.
Let me know in case of any concerns.
Please mark my answer correct/helpful based on impact. This may help other community users to follow correct solution.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-10-2023 12:15 AM
Thank you @AMan1997 for this. I tried this and it created an Attachment, but the attachments in that received email are not included in the attachment that is created by the above script. How to extract attachments from a received email.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-11-2023 08:37 AM
Hi @Thota Naga Jyo1 ,
For extracting the attachments from the received email I wrote the below piece of code:
var grSEA = new GlideRecord('sys_email_attachment'); // Table stores the attachments from emails
grSEA.addEncodedQuery('email.subjectSTARTSWITH' + email.subject); // Queries the matched email
grSEA.query();
while (grSEA.next()) {
if (grSEA.getValue('attachment')) {
var content_type = grSEA.attachment.content_type;
var fname = grSEA.getValue('file_name');
var attachmentSysID = grSEA.getValue('attachment');
var attachmentContentStream = attachment.getContentStream(attachmentSysID);
//gs.info('Attachment content stream: ' + attachmentContentStream);
attachment.writeContentStream(str_gr, fname, content_type, attachmentContentStream);
//gs.info("Attachment created");
}
}
Use this code in the existing script. This will extract the attachments received from email and will attach them to the target table record.
Hope this solution fulfills your requirement.
Thanks,