New Incidents has to be created based on the Email

Sriram Reddy
Tera Contributor

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

 

@AnveshKumar M 

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @Sriram Reddy 

 

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,

 

AnveshKumarM_0-1704358711164.png

 

AnveshKumarM_1-1704358711167.png

 

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.

 

Thanks,
Anvesh

View solution in original post

2 REPLIES 2

AnveshKumar M
Tera Sage
Tera Sage

Hi @Sriram Reddy 

 

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,

 

AnveshKumarM_0-1704358711164.png

 

AnveshKumarM_1-1704358711167.png

 

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.

 

Thanks,
Anvesh