This JavaScript code fetches a CSV file from a NetSuite URL, compares it with an uploaded CSV file, and displays the results in an HTML table. It ensures the CSV file is downloaded directly without opening the search page.
let url =’/app/common/custom/custrecordentrylist.nl?rectype= ID &searchtype=Custom&searchresults.csv?searchid= search ID ‘ // This forces the CSV download without opening the search
// Use fetch or any suitable method to get the response
readCSVFile(file, function (uploadedContent) {
fetch(url,{
method: “GET”,
headers: {
“Accept”: “text/csv”,
“Content-Type”: “text/csv”
},
credentials: “include”
})
.then(response => response.text()) // Get the CSV as text
.then(csvContent => {
let mainCsvData = convertHtmlToCsv(csvContent,”#div__body”);
console.log(‘mainCsvData:’,csvToJson(mainCsvData));
})
.catch(error => console.error(‘Error fetching CSV:’, error));
});
function convertHtmlToCsv(htmlContent,tableSelector) {
let csv = [];
let tempDiv = document.createElement(‘div’);
tempDiv.innerHTML = htmlContent;
// Find the specific table by ID, class, or other selector
let table = tempDiv.querySelector(tableSelector);
// Ensure the table exists
if (!table) {
console.error(‘Table not found’);
return ”;
}
let rows = table.rows;
let csvContent = ”;
// Extract the header row (if it exists)
let headers = rows[0].cells;
let headerRow = [];
for (let i = 1; i < headers.length; i++) { // Skip the first column (Edit | View)
headerRow.push(headers[i].innerText.trim());
}
csvContent += headerRow.join(“,”) + “n“; // Add header row to CSV content
// Loop through each data row and extract cell data
for (let i = 1; i < rows.length; i++) { // Start from 1 to skip the header row
let row = rows[i];
let rowContent = [];
// Loop through each cell in the row (skip the first column)
for (let j = 1; j < row.cells.length; j++) { // Skip the first column (Edit | View)
rowContent.push(row.cells[j].innerText.trim()); // Extract and trim text
}
// Join the row content with commas and append to the CSV content
csvContent += rowContent.join(“,”) + “n“;
}
return csvContent;
}