Attachment from email is not attaching to Incident table with Inbound Email Action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2024 03:37 AM
Hi All,
I have created Inbound Email action with the script attached to create Incident and it's working fine. Record is creating but If I attach any attachment to email and sending to ServiceNow, the attachment is not attaching to newly created Incident record.
Please suggest.
Thank you
var emailSubject = email.subject; // Get the subject line of the email
var callerEmail = emailSubject.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/)[0]; // Extract email using regex
// Look up user based on the email address
var user = new GlideRecord('sys_user');
user.addQuery('email', callerEmail); // Query for user with the extracted email
user.query();
if (user.next()) {
// User found, proceed to set the caller and affected user
var incident = new GlideRecord('incident');
incident.initialize(); // Initialize a new incident record
incident.caller_id = user.sys_id; // Set the caller to the found user
incident.u_affected_user = user.sys_id; // Set the affected user to the found user
incident.priority = 3; // Set the priority to Moderate
incident.short_description = email.subject; // Set the short description to email subject
incident.description = email.body_text; // Set the description to email body
incident.insert(); // Insert the incident record
} else {
gs.info('User with email ' + callerEmail + ' not found.');
}
var attachment = new GlideRecord('sys_attachment'); // GlideRecord for the sys_attachment table
var emailAttachments = email.getAttachments(); // Get the email attachments
// Check if email attachments exist
if (emailAttachments) {
// Iterate through each attachment and insert it into the Incident table
emailAttachments.forEach(function(emailAttachment) {
// Create a new sys_attachment record for the incident
var newAttachment = new GlideRecord('sys_attachment');
newAttachment.initialize();
newAttachment.table_name = 'incident'; // Set the table to 'incident'
newAttachment.table_sys_id = incident.sys_id; // Link the attachment to the incident using its sys_id
newAttachment.file_name = emailAttachment.file_name;
newAttachment.content_type = emailAttachment.content_type;
newAttachment.size_bytes = emailAttachment.size_bytes;
// Copy the attachment content from the email to the new record
newAttachment.setAttachmentData(emailAttachment);
// Insert the new attachment record
newAttachment.insert();
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2024 03:49 AM
why not use GlideSysAttachment.copy?
var emailSubject = email.subject; // Get the subject line of the email
var callerEmail = emailSubject.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/)[0]; // Extract email using regex
// Look up user based on the email address
var user = new GlideRecord('sys_user');
user.addQuery('email', callerEmail); // Query for user with the extracted email
user.query();
if (user.next()) {
// User found, proceed to set the caller and affected user
var incident = new GlideRecord('incident');
incident.initialize(); // Initialize a new incident record
incident.caller_id = user.sys_id; // Set the caller to the found user
incident.u_affected_user = user.sys_id; // Set the affected user to the found user
incident.priority = 3; // Set the priority to Moderate
incident.short_description = email.subject; // Set the short description to email subject
incident.description = email.body_text; // Set the description to email body
incident.insert(); // Insert the incident record
GlideSysAttachment.copy('sys_email',current.sys_id,'incident',incident.sys_id);
} else {
gs.info('User with email ' + callerEmail + ' not found.');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 11:17 PM
Hi Ankur,
Thanks for your reply.
Attchement is not attaching to Incident with the help of above script, Can you please sugegst
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2024 11:18 PM
var emailSubject = email.subject; // Get the subject line of the email
var callerEmail = emailSubject.match(/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/)[0]; // Extract email using regex
// Look up user based on the email address
var user = new GlideRecord('sys_user');
user.addQuery('email', callerEmail); // Query for user with the extracted email
user.query();
if (user.next()) {
// User found, proceed to set the caller and affected user
var incident = new GlideRecord('incident');
incident.initialize(); // Initialize a new incident record
incident.caller_id = user.sys_id; // Set the caller to the found user
incident.u_affected_user = user.sys_id; // Set the affected user to the found user
incident.priority = 3; // Set the priority to Moderate
incident.short_description = email.subject; // Set the short description to email subject
incident.description = email.body_text; // Set the description to email body
incident.insert(); // Insert the incident record
GlideSysAttachment.copy('sys_email',current.sys_id,'incident',incident.sys_id);
} else {
gs.info('User with email ' + callerEmail + ' not found.');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-06-2024 03:59 AM
Hi @Dileep2 ,
As suggested by @Ankur Bawiskar you can use
GlideSysAttachment.copy('sys_email',current.sys_id,'incident',incident.sys_id);
Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand