- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2023 11:31 AM
Hi All,
I have a record on the sys_email table whenever an email is sent from the tool to the servicenow. These records have the body of mail in HTML format. When I right-click on it and click preview mail, then it shows me the whole proper body of the mail. I want to attach this mail body whole in the Work notes of the incident. Basically, I have written an inbound email action that creates an incident as soon as the XML format mail is received in the sys_email table. On the same new incident, the mail (which was seen when clicked on the preview email) should be attached to the incident work note.
Can anyone help me with this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 02:49 AM
Hi @swapnil15
Let's have a look into the post below.
Save Email Text as Attachment
make sure to replace the function createAttachment as following Scott20's comment.
function createAttachment(emailRec, currentRec) {
var fileName = emailRec.subject + '.eml';
// Setup array to push email values into. Add additional as needed/
var emailData = [];
emailData.push("To: " + emailRec.to);
emailData.push("Subject: " + emailRec.subject);
emailData.push("From: " + emailRec.origemail);
emailData.push("Headers: " + emailRec.headers + "Content-Type: multipart/mixed");
emailData.push("");
emailData.push("<html>");
emailData.push("<body>");
emailData.push(emailRec.body_html);
emailData.push("</body>");
emailData.push("</html>");
// Convert emailData to a string separated by new line character.
var emailString = emailData.join(this.newLineChar);
// Create attachment with email string and attach it to the record creatd by the email.
var sysAttachment = new GlideSysAttachment();
sysAttachment.write(currentRec, fileName, this.contentType, emailString);
}
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-31-2023 11:44 PM
Hi @swapnil15
Have you tried to use the email object? email.body_html and email.body_text
Accessing email object variables
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2023 11:00 AM
Hello Tai,
Thanks for your response.
If I use email.body_html, I get the whole text content of the email. I can process that as I want according to the requirement. My aim is the record that I get on sys_email table, it should be converted to file(the whole email) and that converted file should be attached to the newly created incident.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-06-2023 02:49 AM
Hi @swapnil15
Let's have a look into the post below.
Save Email Text as Attachment
make sure to replace the function createAttachment as following Scott20's comment.
function createAttachment(emailRec, currentRec) {
var fileName = emailRec.subject + '.eml';
// Setup array to push email values into. Add additional as needed/
var emailData = [];
emailData.push("To: " + emailRec.to);
emailData.push("Subject: " + emailRec.subject);
emailData.push("From: " + emailRec.origemail);
emailData.push("Headers: " + emailRec.headers + "Content-Type: multipart/mixed");
emailData.push("");
emailData.push("<html>");
emailData.push("<body>");
emailData.push(emailRec.body_html);
emailData.push("</body>");
emailData.push("</html>");
// Convert emailData to a string separated by new line character.
var emailString = emailData.join(this.newLineChar);
// Create attachment with email string and attach it to the record creatd by the email.
var sysAttachment = new GlideSysAttachment();
sysAttachment.write(currentRec, fileName, this.contentType, emailString);
}
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2023 09:15 AM - edited ‎12-11-2023 09:22 AM
@Tai Vu @Sneh
I tried the same approach and proceeded with mail processing but no attachment was found in the newly created incident.
PFB script used:
Script Include -
var emailAsAttachmentUtil = Class.create();
emailAsAttachmentUtil.prototype = {
initialize: function() {
this.newLineChar = "\r\n"; // Microsoft Windows expects \r and \n for return and new line
this.contentType = "text/plain";
},
createAttachment: function(emailRec, currentRec) {
var fileName = emailRec.subject + '.eml';
// Setup array to push email values into. Add additional as needed/
var emailData = [];
emailData.push("To: " + emailRec.to);
emailData.push("Subject: " + emailRec.subject);
emailData.push("From: " + emailRec.origemail);
emailData.push("Headers: " + emailRec.headers + "Content-Type: multipart/mixed");
emailData.push("");
emailData.push("<html>");
emailData.push("<body>");
emailData.push(emailRec.body_html);
emailData.push("</body>");
emailData.push("</html>");
// Convert emailData to a string separated by new line character.
var emailString = emailData.join(this.newLineChar);
// Create attachment with email string and attach it to the record creatd by the email.
var sysAttachment = new GlideSysAttachment();
sysAttachment.write(currentRec, fileName, this.contentType, emailString);
},
type: 'emailAsAttachmentUtil'
};
Inbound Action - (Script Include called at the last )
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
current.urgency = '2';
current.impact = '3';
current.state = '1';
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.caller_id = gs.getUserID();
current.comments = "received from: " + email.origemail + "\n\n" + email.body_text;
current.short_description = email.subject;
//gs.log("Email Body HTML: " + email.body_html);
var htmlcode = email.body_html;
//gs.log("The value of htmlcode is: " + htmlcode, "MyScript"); // You can specify your own log level and log source
var htmlcode = email.body_html;
htmlcode = htmlcode.replace(/<style([\s\S]*?)<\/style>/gi, '');
htmlcode = htmlcode.replace(/<script([\s\S]*?)<\/script>/gi, '');
htmlcode = htmlcode.replace(/<\/div>/ig, '\n');
htmlcode = htmlcode.replace(/<\/li>/ig, '\n');
htmlcode = htmlcode.replace(/<li>/ig, ' * ');
htmlcode = htmlcode.replace(/<\/ul>/ig, '\n');
htmlcode = htmlcode.replace(/<\/p>/ig, '\n');
htmlcode = htmlcode.replace(/<br\s*[\/]?>/gi, "\n");
htmlcode = htmlcode.replace(/<[^>]+>/ig, '');
htmlcode = htmlcode.replace(' ', '');
htmlcode = htmlcode.replace(/\r?\n|\r/g, '');
// gs.log("final email String " + htmlcode);
var indesREG = new SNC.Regex('/Description.*Offence Reason/si'); // this line will get the value of string between Category: and Short Description
// gs.log('indesREG' + indesREG);
var finalvalue = indesREG.match(htmlcode).toString();
var regex = /(?:Description)(.*)(?:Offence Reason)/g;
// gs.log('reg value is' + regex);
finalvalue = regex.exec(finalvalue);
// gs.log('normal final value' + finalvalue);
// gs.log('JN FinalValue ' + finalvalue[1]);
if (finalvalue) {
//gs.log('enter if loop of finalvalue');
var description = finalvalue[1].trim(); // Get the description and remove leading/trailing spaces
current.description = description; // Set the incident's description field
}
var emailAttachment = new global.emailAsAttachmentUtil();
emailAttachment.createAttachment(email, current);
})(current, event, email, logger, classifier);
The incident is created with all the other changes working correctly but the attachment file is not found on the incident. What can be the possible reason ? Or am I missing something.
Also how can I know the size of the file that it is trying to attach? currently the size limitation is 1024MB.
PFA Snaps.
PS - I changed the function as per the Scotts comment but it did not worked.