- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-03-2024 09:52 PM
Hi,
Can you please help me in the below task.
"I have to create an Inbound Email Action where an email is triggered with an Excel file. Based on the records in the Excel file, new incidents have to be created. "
Please help me with the complete process.
Regards,
Sriram Reddy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 01:00 AM
Please find the Screenshots and Script below for the inbound actions, You can modify the script as per your need.
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var email_log = new GlideRecord('sys_email');
email_log.addQuery('uid', email.uid);
email_log.query();
if (email_log.next()) {
var attachLogMail = new GlideRecord('sys_email_attachment');
attachLogMail.addQuery("email", email_log.sys_id);
attachLogMail.query();
while (attachLogMail.next()) {
var file_name = attachLogMail.getValue('file_name');
var ext = file_name.split('.');
ext = ext[ext.length -1];
if (ext == 'xlsx' || ext == 'xls') {
var attachLog = new GlideRecord('sys_attachment');
attachLog.addQuery('sys_id', attachLogMail.attachment);
attachLog.query();
while (attachLog.next()) {
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment()
var attachmentStream = attachment.getContentStream(attachLog.sys_id);
parser.parse(attachmentStream);
//retrieve the column headers
var headers = parser.getColumnHeaders();
var short_description = headers[0];
var description = headers[1];
while (parser.next()) {
var row = parser.getRow();
//I'm using only 2 columns
var incGr = new GlideRecord("incident");
incGr.initialize();
incGr.short_description = row[short_description];
incGr.description = row[description];
incGr.insert();
}
}
} else {
gs.info("MAK: Not a supported file type.");
}
}
} else {
gs.info("MAK: email not found.");
}
})(current, event, email, logger, classifier);
Test excel sheet I used is like this,
Please mark my answer helpful and accept as a solution if it helped 👍✔️
You can create a question for the same and mention me in that question , so that I can add all these there too, it will help me for an accepted answer.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 12:28 AM
Hi @Sriram Reddy
https://www.servicenow.com/community/developer-forum/want-to-send-attchment-excel-sheet-through-inbo...
https://www.servicenow.com/community/now-platform-articles/import-excel-file-from-inbound-email-via-....
Please have a look at these threads it might be helpful for you.
Also, please hit helpful if this resolves your query.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-04-2024 01:00 AM
Please find the Screenshots and Script below for the inbound actions, You can modify the script as per your need.
(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
var email_log = new GlideRecord('sys_email');
email_log.addQuery('uid', email.uid);
email_log.query();
if (email_log.next()) {
var attachLogMail = new GlideRecord('sys_email_attachment');
attachLogMail.addQuery("email", email_log.sys_id);
attachLogMail.query();
while (attachLogMail.next()) {
var file_name = attachLogMail.getValue('file_name');
var ext = file_name.split('.');
ext = ext[ext.length -1];
if (ext == 'xlsx' || ext == 'xls') {
var attachLog = new GlideRecord('sys_attachment');
attachLog.addQuery('sys_id', attachLogMail.attachment);
attachLog.query();
while (attachLog.next()) {
var parser = new sn_impex.GlideExcelParser();
var attachment = new GlideSysAttachment()
var attachmentStream = attachment.getContentStream(attachLog.sys_id);
parser.parse(attachmentStream);
//retrieve the column headers
var headers = parser.getColumnHeaders();
var short_description = headers[0];
var description = headers[1];
while (parser.next()) {
var row = parser.getRow();
//I'm using only 2 columns
var incGr = new GlideRecord("incident");
incGr.initialize();
incGr.short_description = row[short_description];
incGr.description = row[description];
incGr.insert();
}
}
} else {
gs.info("MAK: Not a supported file type.");
}
}
} else {
gs.info("MAK: email not found.");
}
})(current, event, email, logger, classifier);
Test excel sheet I used is like this,
Please mark my answer helpful and accept as a solution if it helped 👍✔️
You can create a question for the same and mention me in that question , so that I can add all these there too, it will help me for an accepted answer.
Anvesh