CSV File Generation & Storage using suitescript

This function generates a CSV file, saves it in the NetSuite File Cabinet, and sends it via email.

Step 1: Define CSV Headers

javascript

Copy

Edit
const headers = [
    'Item Name', 'Description', 'Strand Count', 'Foot', 'Fiber Grade', 'Class', 'Fiber Mode Type',
    'Components Item #1', 'Components Item #1 Qty', 'Components Item #2', 'Components Item #2 Qty',
    'Components Item #3', 'Components Item #3 Qty', 'Components Item #4', 'Components Item #4 Qty',
    'Components Item #5', 'Components Item #5 Qty', 'Components Item #6', 'Components Item #6 Qty',
    'Components Item #7', 'Components Item #7 Qty', 'Components Item #8', 'Components Item #8 Qty',
    'Components Item #9', 'Components Item #9 Qty', 'Components Item #10', 'Components Item #10 Qty',
    'Price Level Base Price', 'Price Level Distributor', 'Price Level International', 'Price Level International NS',
    'Price Level BBY', 'Class L5', 'Class L6', 'Class Tier Two', 'Fiber Type Facet', 'Connection Type Facet',
    'Cable Construction Facet', 'Fiber Grade Facet',
];
  • The CSV headers represent the column names in the CSV file.

Step 2: Define CSV Rows (Data)

javascript

Copy

Edit
const rows = [
    [
        '3SOM3-1SC-1SC-100m-RAUUESSF', 'Custom Patch Cable-S50125MOM3R-1xSC-1xSC-PullEye=Standard-100m/329ft',
        '1', '331', 'OM3', 'Cleerline FG : Cables : Fiber Patch Cables : Custom Assemblies : Multimode',
        'Multi Mode', 'SSF Simplex Cable : S50125MOM3R-B', '331', 'OTR-SCSXMM3.0', '1',
        'CTG-MFG OVERHEAD', '1', 'CTG-PATCH-FANOUT', '1', 'NO-FURCATION', '1', 'OTR-SCSXMM3.0', '1',
        'CTG-MFG OVERHEAD', '1', 'CTG-PATCH-FANOUT', '1', 'NO-FURCATION', '1', 'PULLEYED-PATCH CABLE',
        '1', '118.88', '95.44', '95.44', '95.44', '118.88', 'Simplex', 'MultiMode', 'Custom Assemblies',
        'MultiMode', 'SC/UPC', 'Simplex', 'OM3'
    ],
];
  • This array represents the row values for the CSV file.
  • Each row contains item details, including:
  • Item name, description, quantity, fiber type, class, components, price levels, and facets.

Step 3: Create CSV Content

javascript

Copy

Edit
var csvContent = headers.join(',') + 'n'; // Add headers
rows.forEach(function (row) {
    csvContent += row.join(',') + 'n'; // Add each row
});
  • This converts the headers and rows into CSV format.
  • The .join(',') method ensures that columns are separated by commas.

Step 4: Create CSV File

javascript

Copy

Edit
const csvFile = file.create({
    name: 'Item_Import' + 'newItemName' + '.csv',
    fileType: file.Type.CSV,
    contents: csvContent,
    folder: 1465256,
    isOnline: true
});
  • Creates a new CSV file named “Item_ImportnewItemName.csv”.
  • The file is stored in folder ID 1465256 in NetSuite’s File Cabinet.
  • isOnline: true makes the file accessible.

Step 5: Save File to NetSuite

javascript

Copy

Edit
const fileId = csvFile.save();
log.debug('File Saved', 'File ID: ' + fileId);
  • Saves the file to NetSuite’s File Cabinet.
  • Logs the file ID for debugging.

Step 6: Load File for Email Attachment

javascript

Copy

Edit
const savedFile = file.load({
    id: fileId,
});
  • Loads the saved file using its ID.

Step 7: Define Email Recipients

javascript

Copy

Edit
const recipientEmail = 'sruthi.krishna@jobinandjismi.com';
const senderEmail = 27393;
  • Recipient Email: The CSV will be sent to sruthi.krishna@jobinandjismi.com.
  • Sender Email: Uses the employee ID 27393 as the sender.

Step 8: Send Email with Attachment

javascript

Copy

Edit
email.send({
    author: senderEmail,
    recipients: recipientEmail,
    subject: 'Item Import CSV File',
    body: 'Please find attached the CSV file for item import into NetSuite.',
    attachments: [savedFile],
});
log.debug('Email Sent', 'CSV file sent to: ' + recipientEmail);
  • Sends an email with:
  • Sender: senderEmail (employee ID 27393).
  • Recipient: sruthi.krishna@jobinandjismi.com.
  • Subject: "Item Import CSV File".
  • Body: "Please find attached the CSV file for item import into NetSuite.".
  • Attachment: The saved CSV file.

Leave a comment

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