Inbound Email Action not triggering Scheduled Import via Data Source (Works fine in Background Scrip
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
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);
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
Hi @Tanushree Maiti i tried but still it is not working. it is not populating the incident records from attachments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
i was using this bg script, I just need to make it dynamic.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Tuesday
check below links
Auto load Excel spreadsheet using Email Inbound Action
Loading data from an email attachment
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader