Inbound Email Action not triggering Scheduled Import via Data Source (Works fine in Background Scrip

rah dev
Tera Contributor

Hello Community,I am working on a requirement where an incoming email with a specific subject contains a CSV attachment. I need to parse this CSV data and map it to a custom table to create records.To test the logic, I wrote a Background Script using a hardcoded sys_email record. The script works flawlessly—it deletes old attachments from the Data Source, copies the new one from the email, and triggers the Scheduled Import. However, when I place this exact logic inside an Inbound Email Action, it fails to copy the attachment and process the import

(function runAction(current, event, email, logger, classifier) {

var dataSourceID = 'YOUR_DATA_SOURCE_SYS_ID';
var scheduledImportID = 'YOUR_SCHEDULED_IMPORT_SYS_ID';

if (current.hasAttachments()) {

// 1. Delete previous attachments on the Data Source to prevent importing old data
var grAttach = new GlideRecord('sys_attachment');
grAttach.addQuery('table_name', 'sys_data_source');
grAttach.addQuery('table_sys_id', dataSourceID);
grAttach.query();
while (grAttach.next()) {
var attach = new GlideSysAttachment();
attach.deleteAttachment(grAttach.sys_id);
}

// 2. Copy the new attachment from the incoming email to the Data Source
var copiedCount = GlideSysAttachment.copy('sys_email', current.sys_id, 'sys_data_source', dataSourceID);

// 3. Trigger the Scheduled Import programmatically
var grImp = new GlideRecord("scheduled_import_set");
if (grImp.get(scheduledImportID)) {
gs.executeNow(grImp);
logger.info("Successfully copied attachment and triggered Scheduled Import.");
} else {
logger.error("Error: Scheduled Import record not found with sys_id: " + scheduledImportID);
}
} else {
logger.error("Error: Email received, but no attachments were found.");
}

})(current, event, email, logger, classifier);


When running via Inbound Email Action, current.hasAttachments() returns false, or GlideSysAttachment.copy() fails because the attachments are not yet fully committed to the sys_attachment table at the moment the Inbound Action executes.Is there a timing issue or order of execution conflict between attachment processing and Inbound Actions? What is the best practice to handle email attachment processing into a Data Source? Should I move this logic to an After Async Business Rule on the sys_email table instead?Any guidance or workarounds would be highly appreciated!Thanks in advance.

4 REPLIES 4

Tanushree Maiti
Tera Patron

Hi @rah dev 

Can you change this line and try once.

From : 

var copiedCount = GlideSysAttachment.copy('sys_email', current.sys_id, 'sys_data_source', dataSourceID);

 

To:

var copiedCount = GlideSysAttachment().copy('sys_email', current.sys_id, 'sys_data_source', dataSourceID);

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi @Tanushree Maiti i tried but still it is not working. it is not populating the incident records from attachments.

rah dev
Tera Contributor

i was using this bg script, I just need to make it dynamic.

(function runAction(current, event, email, logger, classifier) {

var dataSourceID = 'data source id';
    var scheduledImportID = 'scheduled import';

    // Verify the incoming email actually has an attachment
    var sysEmailGr = new GlideRecord('sys_email');
    if (sysEmailGr.get('sysid of  sys_email') && sysEmailGr.hasAttachments()) {

        // 1. Delete previous attachments on the Data Source to prevent importing old data
        var grAttach = new GlideRecord('sys_attachment');
        grAttach.addQuery('table_name', 'sys_data_source');
        grAttach.addQuery('table_sys_id', dataSourceID);
        grAttach.query();
        while (grAttach.next()) {
            var attach = new GlideSysAttachment();
            attach.deleteAttachment(grAttach.sys_id);
        }

        // 2. Copy the new attachment from the incoming email to the Data Source
        var copiedCount = GlideSysAttachment().copy('sys_email', 'sys_id_sysemail', 'sys_data_source', dataSourceID);

        // if (copiedCount > 0) {
            // 3. Trigger the Scheduled Import programmatically
            var grImp = new GlideRecord("scheduled_import_set");
            if (grImp.get(scheduledImportID)) {
               gs.info("line25");
                gs.executeNow(grImp); // Execute the import and transform process
                gs.log("Successfully copied Amazon Connect attachment and triggered Scheduled Import.");
            } else {
                gs.log("Error: Scheduled Import record not found with sys_id: " + scheduledImportID);
            }
        // } else {
        //     gs.log("Error: Failed to copy the CSV attachment from email to the Data Source.");
        // }
    } else {
        gs.log("Error: Email received, but no attachments were found.");
    }
})(current, event, email, logger, classifier);

Ankur Bawiskar
Tera Patron

@rah dev 

check below links

Auto load Excel spreadsheet using Email Inbound Action 

Loading data from an email attachment 

https://www.servicenow.com/community/developer-forum/inbound-action-to-import-attachment/td-p/315884...

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader