Can I use Inbound Email Action to clear a table and trigger Data Source import?

Pawel Barcik
Mega Guru

Hi everyone,

 

I’m trying to automate a process using an Inbound Email Action. Here’s what I want to achieve:

1. When an email is received (with an attachment),

2. I include the sys_id of the Data Source in the email body (e.g., as plain text),

3. The script should:

• Delete all records from a specific custom table (u_u_cmdb_g_s),

• Remove any existing attachments from the Data Source (sys_data_source),

• Save the new attachment from the email into that Data Source,

• Trigger the import set to load data from the attachment,

• Automatically run the Transform Map to populate the target table.

 

The idea is to fully refresh the target table every time a new file is emailed in.

 

Is it possible to do all of this from within a single Inbound Email Action?

Would it be better to move some logic into a Script Include and call it from there?

 

Any advice, best practices, or working examples would be much appreciated. Thank you!

 

 Script include 

var EmailImportRunner = Class.create();
EmailImportRunner.prototype = {
initialize: function() {},

run: function(dataSourceSysId, attachments) {
var grTarget = new GlideRecord('u_u_cmdb_g_s');
grTarget.query();
while (grTarget.next()) grTarget.deleteRecord();

var dsGR = new GlideRecord('sys_data_source');
if (!dsGR.get(dataSourceSysId)) return;

var oldAtt = new GlideRecord('sys_attachment');
oldAtt.addQuery('table_name', 'sys_data_source');
oldAtt.addQuery('table_sys_id', dataSourceSysId);
oldAtt.query();
while (oldAtt.next())
new GlideSysAttachment().deleteAttachment(oldAtt.getUniqueValue());

var sa = new GlideSysAttachment();
for (var i = 0; i < attachments.length; i++)
sa.write('sys_data_source', dataSourceSysId, attachments[i].getFileName(), attachments[i].getContent());

var imp = new GlideImportSetRunner();
imp.setDataSource(dsGR);
imp.setRunTransformImmediately(true);
imp.execute();
},

type: 'EmailImportRunner'
};

 

 

email action 

 

 

(function runInboundEmailActions(email, email_action, event) {
var dsSysId = '59b6bd3bc3a1e210da501d33e40131e9';

var runner = new EmailImportRunner();
var result = runner.run(dsSysId, email.attachments);

email.setSubject("Import zakończony");
email.setBodyText(result);
})(email, email_action, event);

2 REPLIES 2

Uncle Rob
Kilo Patron

Its a good practice to put code like that into a Script Include.
That way its more accessible for re-use.
Like today you have a single vector (email) to <do all that stuff>.  Tomorrow they'll be asking for a UI Action as well.
Or a similar option that uses much of the same code base but different email conditions.

Use the Inbound Action as the trigger/catalyst and have it call the Script Include to do the heavy lifting.

Currently what I did doesn't work, I don't know why I have script include and inbaud action email as above.