Inbound Email Action - Attach inbound attachment to existing record

Patrick Farr
Tera Contributor

I'm attempting to create a data source with an attachment that will be replaced daily by an inbound email action.  Our instance receives an email every morning with an attached spreadsheet, at which point we need to delete the existing spreadsheet on our data source and replace it with the email's.  I understand that a new record is effectively initialized when an inbound email action is triggered by a new email, and the new record would have the item attached to it.  Is there a way to point this attachment to the existing record instead?  I'm currently using a gliderecord query in my inbound email action script in order to delete the attachment associated with the existing data source first, which seems to be working, but I cannot seem to get the attachment moved from the email to the existing record:

 

var attachment = new GlideRecord('sys_attachment');
var dataSource = new GlideRecord('sys_data_source');
var attachUtil = new GlideSysAttachment();

dataSource.get('name', 'ADP Terminated Users');
gs.log(dataSource.sys_id);
if(attachment.get('table_sys_id', dataSource.sys_id.toString())){
gs.log(attachment.sys_id);
attachUtil.deleteAttachment(attachment.sys_id.toString());
}

gs.log('inbound action check: ' + current.sys_id + ' ' + dataSource.sys_id.toString());
attachUtil.copy('sys_data_source', current.sys_id, 'sys_data_source', dataSource.sys_id.toString());

 

Presumably this is because the 'current' record hasn't actually been inserted into the table yet.  Is there a simpler way to set the target for the attachment, or is this the right track?  If so, what needs to be done?

 

Edit:  Found an answer, switched 'sys_data_source' and current.sys_id in the source parameters to 'sys_email' and sys_email.sys_id, it's working now.  Is it appropriate to delete this question or just leave it with the answer?

2 REPLIES 2

sachin_namjoshi
Kilo Patron
Kilo Patron

I suggest to mark this question as as answered so that others can benefit from your solution.

 

Regards,

Sachin

martinaparicio
Tera Contributor

I also had this requirement because the file I receive from our external source is zipped and I needed to replace the attachment in an existing data source.  My inbound email action script is:

// 1. First step deletes existing attachment in the data source
var dsid = '995c28e4dbdbfb008d11fb0e0f9619d3'; //data source sys_id
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',dsid);
gr.query();
if(gr.next()){
  gr.deleteMultiple();
}  

//2.This command copies the email attachment to the data source.
// sys_email.sys_id is the email sys_id processed by the inbound action
// and copies to the data source.
GlideSysAttachment.copy('sys_email',sys_email.sys_id,'sys_data_source',dsid);

We have high confidence in the email attachment received and data source not changing.