Attachment from email is not attaching to Incident table with Inbound Email Action
Options
- 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();
});
}
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 02:22 AM
have achieved the solution with the attached script
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();
var incidentSysID; // Declare the incident Sys ID variable
if (user.next()) {
// User found, proceed to set the caller and affected user
current.caller_id = user.sys_id;
current.u_affected_user = user.sys_id;
current.priority = 3;
current.short_description = email.subject;
current.description = email.body_text;
current.location = gs.getUser().getLocation();
current.u_phone = gs.getUser().getRecord().getValue('phone');
//current.assignment_group = 'd3059792131fd240adce3b27d144b07d';
//current.assignment_group = "KRONOS - US";
//current.assignment_group.sys_id='d3059792131fd240adce3b27d144b07d';
current.assignment_group.name = "KRONOS - US";
//current.assignment_group.setDisplayValue('KRONOS - US');
current.insert();
} else {
gs.info('User with email ' + callerEmail + ' not found.');
}