The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Load Attachment & Run the import set from email

sainath3
Mega Guru

Hi Team,

 

Requirement is: When user send an email with attachment. that data (only open Incidents) should reflect in target table.

ex: excel Attachment.

Incident numberShort descriptionDescriptionState
123keyborad issueKeyborad buttorns are not workingopen
456RAM issueLaptop is not workingopen
789Graffic card issueissue with graffic cardResolved

 

these records should get run through transform map.

 

we have one data source with transform map, so whatever the attachment is coming from email that should get load to import set table and trigger the transform map.

 

Can anyone share me Ideas or any examples if you find.

 

2 REPLIES 2

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Please follow the steps mentioned in the below article:-

 

https://www.servicenow.com/community/developer-blog/loading-data-from-an-email-attachment/ba-p/22797...

 

Please mark my answer as correct based on Impact.

sainath3
Mega Guru

Hi Team.

 

I have used the same code mentioned above.

but the attachment is not added to the table.

 

Inbound email Action:

 

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 
var importSetTableName = "u_gcb_load";
      var transformMapIDs = "b12aaf5597132110f2257ca0f053afac";   //Use a comma to specify multiple transform maps
      var applicatonScope = "Global";
 
      // Create the data source record
      current.name = "File import from:" + email.from;   //Feel free to rename this as appropriate
      current.import_set_table_name = importSetTableName;
      current.file_retrieval_method = "Attachment";
      current.type = "File";
      current.format = "Excel"; // For Excel Files
      //current.format = "CSV"; // For CSV Files
      current.header_row = 1;
      current.sheet_number = 1;
      current.sys_package.setDisplayValue(applicatonScope);
      current.sys_scope.setDisplayValue(applicatonScope);
      var dataSourceID = current.insert();
     
      /*
       * Schedule Load of Attachment
       *
       * This inbound email action will generate an import data source, however the attachment isn't copied to the data source until
       * after the insert of the record.   Scheduling the import to happen 30 seconds later so that attachment has time to be copied.
       */
      new global.EmailFileImportUtils().scheduleImport(dataSourceID, transformMapIDs);
 
})(current, event, email, logger, classifier);
 
 
 
Script Include:
 
var EmailFileImportUtils = Class.create();
EmailFileImportUtils.prototype = {
initialize: function() {
},
 
scheduleImport: function(dataSourceID, transformMapIDs) {
 
gs.log("GCB Source ID"+dataSourceID);
/*
  * Create scheduled job to process import
  *
  * The inbound email action will generate an import data source, however the attachment isn't copied to the data source until
  * after the insert of the record.   The code below will create a scheduled job to process the import 30 seconds later
  * so that attachment has time to be copied to the data source from the email.
  */
 
var schRec = new GlideRecord("sys_trigger");
schRec.name = "Load Data Source: " + dataSourceID;
schRec.trigger_type = 0;   // Run Once
schRec.script = "new global.EmailFileImportUtils().loadImportSet('" + dataSourceID + "', '" + transformMapIDs + "')";
 
var nextAction = new GlideDateTime();
nextAction.addSeconds(30);   // 30 seconds should be enough time however this can be changed.
schRec.next_action = nextAction;
schRec.insert();
},
 
loadImportSet: function(dataSourceID, transformMapIDs) {
// Get Datasource Record
var dataSource = new GlideRecord("sys_data_source");
dataSource.get(dataSourceID);
 
// If CSV and header isn't on row 1, recreate attachment with empty rows removed, and delete original
if (dataSource.getValue("format") == "CSV" && dataSource.getValue("header_row") > 1) {
var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery("table_sys_id", dataSource.getValue("sys_id"));
attachmentRec.query();
if (attachmentRec.next()) {
var oldAttachmentID = attachmentRec.getValue("sys_id");
var inputStream = new GlideSysAttachment().getContentStream(oldAttachmentID);
var textReader = new GlideTextReader(inputStream);
var ln = " ";
var newLine = "";
 
var lineCounter = 0;
var headerRow = parseInt(dataSource.getValue("header_row"));
while((ln = textReader.readLine()) != null) {
lineCounter += 1;
if (lineCounter < headerRow) {
continue;
}
 
if (ln.length > 1) {
newLine += ln + "\n";
}
}
new GlideSysAttachment().write(dataSource, 'Changed ' + attachmentRec.getValue('file_name'), "text/csv", newLine);
//Delete original email attachment so that the new one is processed.  If two remain, API may get confused on which one to process.
attachmentRec.deleteRecord();
}
}
 
// Process data source file
var loader = new GlideImportSetLoader();
var importSetRec = loader.getImportSetGr(dataSource);
var ranload = loader.loadImportSetTable(importSetRec, dataSource);
importSetRec.state = "loaded";
importSetRec.update();
 
// Transform import set
var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, transformMapIDs);
transformWorker.setBackground(true);
transformWorker.start();
},
 
type: 'EmailFileImportUtils'
 
};
sainath3_0-1686155632617.png