Auto-load emailed CSV File report into custom table(***) using Inbound action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi all,
I had a requirement where business sends CSV file as an email attachment and I need to load that data into a custom table automatically.
The emails will only come from a specific user profile: Praveen which Inbound email
The report is sent as an attachment (CSV/Excel from the User
Data should be staged first (stagging table)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
the link I shared handles what you want -> staging table, transform map
what didn't work for you from that link?
You mentioned in your question you want data in staging table but now you are saying it should be direct
If you don't want data in staging then parse CSV file using CSVParser and then directly load data into target table
💡 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 || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Ankur Bawiskar below script i am using but getting error in logs - did not create or update table using current
(function runAction(email, email_action, event) {
var targetTable = "x_att2_cus_ad_plan_mow_ptt";
// Map CSV column names → table field names
// Keys are LOWERCASE versions of CSV header text
var fieldMap = {
"type": "u_type", "clci id": "u_clci_id","circuit id": "u_circuit_id", "ptt re": "u_ptt_ref","customer Name": "u_customer_name","city": "u_city","country": "u_country","service": "u_service","line Speed": "u_line_speed","status": "u_status","start date": "u_start_date","end date": "u_end_date","start time": "u_start_time","end time": "u_end_time","timezone": "u_timezone","impact duration": "u_impact_duration","mcn":"u_mcn","Address Line": "u_address_line",
"Sharepoint link": "u_sharepoint_link","FEDGOV indicator": "u_fedgov_indicator",
"MCN Cust_Id": "u_mcn___cust_id","MCN - Cust Id - Us Restricted": "u_mcn___cust_i__us_restricted","MCN - Cust Id - Fed Ramp": " u_mcn___cust_id___fed_ramp","region": "u_region","Date Created":"sys_created_on",
};
// 1. Get the first attachment from this email
var attGR = new GlideRecord('sys_attachment');
attGR.addQuery('table_sys_id', email.sys_id); // attachments on this email
//attGR.orderByD('sys_created_on');
attGR.orderByDesc('sys_created_on');
attGR.query();
if (!attGR.next()) {
gs.info('PTT Inbound: No attachment found on email ' + email.sys_id);
return;
}
gs.info('PTT Inbound: Inserted ' + insertedCount + ' rows into x_att2_pma_mow_ptt from attachment ' + attGR.sys_id);
var sa = new GlideSysAttachment();
var headers = [];
while (attach.next()) {
var stream = sa.getContentStream(attach.sys_id.toString());
if (!stream) {
gs.info("[CSV Loader] No stream for attachment: " + attach.sys_id);
continue;
}
var reader = new GlideTextReader(stream);
var line;
var lineCount = 0;
while ((line = reader.readLine()) !== null) {
line = line.trim();
if (line === "") {
continue;
}
// Simple CSV split (no quoted-comma handling)
var parts = line.split(',');
// First line = header row
if (lineCount === 0) {
headers = [];
for (var i = 0; i < parts.length; i++) {
var header = parts[i].replace(/\s+/g, ' ').trim();
headers.push(header);
}
gs.info("[CSV Loader] Headers: " + headers);
} else {
// Data row → insert into table
var gr = new GlideRecord(targetTable);
gr.initialize();
for (var j = 0; j < headers.length; j++) {
var rawHeader = headers[j];
var headerKey = rawHeader.toLowerCase(); // used for fieldMap
var fieldName = fieldMap[headerKey];
var value = (parts[j] || "").trim();
if (fieldName && value !== "") {
gr.setValue(fieldName, value);
}
gs.info("[CSV Loader] Row " + lineCount + " - " + rawHeader + " (" + fieldName + ") = " + value);
}
var newSysId = gr.insert();
gs.info("[CSV Loader] Inserted record into " + targetTable + " with sys_id: " + newSysId);
}
lineCount++;
}
}
gs.info("[CSV Loader] CSV processing completed for RITM: " + ritmSysId);
})(email, email_action, event);