Suitescript code to fetch required details as array from CSV file

Consider that columns Document No. and Internal ID need to be fetched from the CSV file.

let fileObj = file.load({
    id: '/csv-file-path',
});
let csvContent = fileObj.getContents();
const rows = csvContent.split('n'); // Split CSV content into rows
const intidIndex = 0;//index of the required column
const docNoIndex = 1;//index of the required column
if (intidIndex === -1 || docNoIndex === -1) {
    log.error('Column not found');
    return [];
}
const columnValues = [];
for (let i = 1; i < rows.length; i++) { // Start from index 1 to skip headers
    const row = rows[i].split(',');
    if (row.length > intidIndex && row.length > docNoIndex) {
        let objValue = {};
        objValue["Document No"] = row[docNoIndex].trim();
        objValue["Internal ID"] = row[intidIndex].trim();
        columnValues.push(objValue); // Push value of the desired column to columnValues array
    }
}
log.debug("columnValues",columnValues)//the final array that contains required values

Leave a comment

Your email address will not be published. Required fields are marked *