Skip to content

Convert a CSV file into object to handle CSV more elegantly #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Attachments/CSVParser/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/********************************************************************************/
Input:
attachmentQuery - Pass sysId CSV attachment

Output:
converted into object from CSV

/********************************************************************************/

function parseCSVFile(sysId) {
var attachmentRecord = new GlideRecord("sys_attachment");
attachmentRecord.get(sysId);
attachmentRecord.query();

if (attachmentRecord.next()) {
var stringUtil = new GlideStringUtil();
var sysAttachment = new GlideSysAttachment();
var bytesData = sysAttachment.getBytes(attachmentRecord);
var encData = stringUtil.base64Encode(bytesData);
var decData = stringUtil.base64Decode(encData) + '';

var delimiter = ',';
var quoteCharacter = '"';
var csvArray = decData.split("\r\n");

var index = 0
var result = [];
for (index = 0; index < csvArray.length; index++) {
var row = csvArray[index];
if (row) {
var csvParser = new sn_impex.CSVParser().parseLineToArray(row, delimiter, quoteCharacter);
var rowObject = {};
for (var i = 0; i < csvParser.length; i++) {
rowObject['column' + (i + 1)] = csvParser[i];
}
result.push(rowObject);
}
}
return result;
}
}
Loading