How to read the attached excel file from workflow.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 07:50 AM
Hi All,
After raising the request I need to read the attached excel file through workflow ? how to do that I am doing but it is not reading the data.
here is my code--
var gra = new GlideRecord('sys_attachment');
gra.addQuery('table_sys_id', current.sys_id);
gra.addQuery('table_name', 'sc_req_item');
gra.addQuery('content_type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
gra.query();
var foundExcel = false;
var workNoteMessages = [];
if (gra.next()) {
foundExcel = true;
var parser = new sn_impex.GlideExcelParser();
var attach = new GlideSysAttachment();
var attachmentStream = attach.getContentStream(gra);
parser.setHeaderRowNumber(2); // 3rd row is the header (0-based index)
parser.setSource(attachmentStream);
parser.parse();
var fieldMap = {
'API_Version': 'version',
'API_Name': 'name'
// Add more mappings if needed
};
var headers = parser.getColumnHeaders();
if (!headers || headers.length === 0) {
var noHeadersMsg = 'No headers found in Excel file. Check if row 3 contains column names.';
gs.warn(noHeadersMsg);
workNoteMessages.push(noHeadersMsg);
} else {
gs.info('Excel Headers: ' + headers.join(', '));
while (parser.next()) {
// Skip empty rows
var isEmptyRow = headers.every(function(header) {
return !parser.getValue(header);
});
if (isEmptyRow) continue;
var apiRecord = new GlideRecord('cmdb_ci_api');
apiRecord.initialize();
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
var fieldName = fieldMap[header];
var value = parser.getValue(header);
if (fieldName && value) {
apiRecord.setValue(fieldName, value);
gs.info('Setting ' + fieldName + ' = ' + value);
} else if (!fieldName) {
var warnMsg = 'No field mapping for Excel column: ' + header;
gs.warn(warnMsg);
workNoteMessages.push(warnMsg);
}
}
try {
var sysId = apiRecord.insert();
var successMsg = 'Inserted API record with sys_id: ' + sysId;
gs.info(successMsg);
workNoteMessages.push(successMsg);
} catch (e) {
var errorMsg = 'Insert failed for API record: ' + e.message;
gs.error(errorMsg);
workNoteMessages.push(errorMsg);
}
}
}
}
if (!foundExcel) {
var noAttachmentMsg = 'No Excel (.xlsx) attachment found for RITM: ' + current.number;
gs.warn(noAttachmentMsg);
workNoteMessages.push(noAttachmentMsg);
}
// Write all accumulated messages to work_notes at once
if (workNoteMessages.length > 0) {
current.work_notes = workNoteMessages.join('\n');
current.update();
}
2 REPLIES 2
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2025 08:01 AM
this link has approach
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2025 09:42 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards,
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader