Accepting a file via inbound email attaching to existing data source and updating target table

anfield
Tera Guru

Working on an integration to do the following - accept an inbound email with a certain subject and header and attach that email with spreadsheet to an existing datasource. Automatically trigger the transform map to transform that data into the servicenow target table (or run this from a scheduled job). Also will need to either delete an attachment if one already exists on that datasource (from the last job run).

Found the following URL with a description of how to do this, and followed the steps  but this creates a new datasource each time. Does anyone know how to do this with using an existing datasource?

Here is the link I have been following - Ive setup the inbound action with script, script include, datasource, and the transform map

https://community.servicenow.com/community?id=community_blog&sys_id=908c26e1dbd0dbc01dcaf3231f9619c4

1 ACCEPTED SOLUTION

Tim Provin
Mega Guru

If you want to use an existing Data Source, you should be able to use everything from that other thread, but for your inbound action you will want to do something like this.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

       //Set the Data Source to be used
       var dataSourceID = "SYS ID OF THE DATA SOURCE YOU WANT TO USE";

       //Query the attachments for this data source and delete them
       var gr = new GlideRecord('sys_attachment');
       gr.addQuery('table_name','sys_data_source');
       gr.addQuery('table_sys_id',dataSourceID);
       gr.query();

       gr.deleteMultiple();

       //Copy attachment from the Email to the Data Source
 GlideSysAttachment.copy('sys_email',sys_email.sys_id,'sys_data_source',dataSourceID);

       /*
         * 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);

View solution in original post

8 REPLIES 8

Tim Provin
Mega Guru

If you want to use an existing Data Source, you should be able to use everything from that other thread, but for your inbound action you will want to do something like this.

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

       //Set the Data Source to be used
       var dataSourceID = "SYS ID OF THE DATA SOURCE YOU WANT TO USE";

       //Query the attachments for this data source and delete them
       var gr = new GlideRecord('sys_attachment');
       gr.addQuery('table_name','sys_data_source');
       gr.addQuery('table_sys_id',dataSourceID);
       gr.query();

       gr.deleteMultiple();

       //Copy attachment from the Email to the Data Source
 GlideSysAttachment.copy('sys_email',sys_email.sys_id,'sys_data_source',dataSourceID);

       /*
         * 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);

Tried this as the script used on the inbound data source + the script include from the other URL. The file is not getting attached to the datasource

Well that is because I forgot to add the line to copy the attachment to the data source.  My fault!!!  I have updated the above script to contain that.

Just a quick update since I was thinking about it.  Using the gr.deleteMultiple() to delete the attachment(s) is probably not the best way to go about it.  I would probably replace that line with the following and use the GlideSysAttachment API to handle the delete any attachments on the data source.

while (gr.next()) {
GlideSysAttachment.deleteAttachment(gr.sys_id);
}