Attach CSV with Tabs

ican
Tera Contributor

Use Case:

Attach CSV with tabs.

I know how to attach CSV but how to do tabs?

 

Thanks!

1 ACCEPTED SOLUTION

To Generate CSV Files with Multiple Tabs in ServiceNow :

 

  1. Create Multiple CSV Files: Each tab will be a separate CSV file. You need to write the data for each tab to a different CSV file.

Here’s an example of how you can create multiple CSV files in a ServiceNow script:

 

 

// Example to generate multiple CSV files
var csv1 = "Header1,Header2,Header3\nRow1Col1,Row1Col2,Row1Col3\nRow2Col1,Row2Col2,Row2Col3";
var csv2 = "HeaderA,HeaderB,HeaderC\nRow1ColA,Row1ColB,Row1ColC\nRow2ColA,Row2ColB,Row2ColC";

// Function to create CSV attachment
function createCSVAttachment(csvContent, fileName) {
    var attachment = new GlideSysAttachment();
    var gr = new GlideRecord('sys_attachment');
    var file = new GlideSysAttachment().write(gr, fileName, 'text/csv', csvContent);
    return file;
}

createCSVAttachment(csv1, "Tab1.csv");
createCSVAttachment(csv2, "Tab2.csv");

 

This will create two attachments in the sys_attachment table in ServiceNow which can be downloaded in excel format.

Sanjana8_0-1720011501327.png

 

Combine CSV Files (Optional): If you need to combine the CSV files into a single file, you can use a delimiter (such as ---) to separate the data from different tabs.

 

var combinedCSV = csv1 + "\n---\n" + csv2;
createCSVAttachment(combinedCSV, "CombinedTabs.csv");

 

 

Sanjana8_1-1720011656779.png

 

 

Sanjana8_2-1720011679490.png

 

However to see it as a true tabs( separate sheets under 1 workbook), 

you should use an Excel file format. ServiceNow does not natively support creating Excel files, so you might need to use a script or a custom ServiceNow integration to convert the CSV files into an Excel file with multiple sheets.

Here's an example using the excel js library in a Node.js environment (outside of ServiceNow):

 

 

const ExcelJS = require('exceljs');

async function createExcelFile() {
    const workbook = new ExcelJS.Workbook();
    const sheet1 = workbook.addWorksheet('Tab1');
    const sheet2 = workbook.addWorksheet('Tab2');
    
    // Add rows to the sheets
    sheet1.addRows([
        ['Header1', 'Header2', 'Header3'],
        ['Row1Col1', 'Row1Col2', 'Row1Col3'],
        ['Row2Col1', 'Row2Col2', 'Row2Col3']
    ]);
    
    sheet2.addRows([
        ['HeaderA', 'HeaderB', 'HeaderC'],
        ['Row1ColA', 'Row1ColB', 'Row1ColC'],
        ['Row2ColA', 'Row2ColB', 'Row2ColC']
    ]);

    // Write to file
    await workbook.xlsx.writeFile('MultiSheetExcel.xlsx');
}

createExcelFile();

 

If you want to use a similar approach within ServiceNow, you'll need to look into custom integrations or the use of REST APIs to send data to an external service that can handle Excel file creation, which will be quite a complicate process.

 

I hope this is helpful for you.

 

Thankyou

Sanjana 

 

 

 

View solution in original post

12 REPLIES 12

Hi @ican 

Then better create 2 different CSV for that tabs and attach. Also Table data means, you are adding table data in CSV and then attaching?

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

I have already implemented attaching CSV with table data, but the CSV with tabs is what I am querying here.

This is what my point mate. We need to write a logic which we can pass from SN to CSV which will read the Tabs name and then separate it and attach. Instead of increasing tehcnical debt better create 2 CSV with the name as Tabs. 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Sure, but the requirement is to have multiple tabs in an CSV/Excel.

Can you please elaborate on this one?

Thanks.

Sanjana8
Kilo Guru

Look for the Attachments related list on the form. It's usually located towards the bottom of the form Click on the Attach File button or similar option.

  • Select your CSV file from your local drive.
  • ServiceNow will handle the upload process. Ensure your CSV file with tabs as delimiters is selected.
  • Click Upload or Attach to complete the process.

 

Certainly! When I mentioned "Ensure your CSV file with tabs as delimiters is selected," it refers to making sure that the CSV file you are attaching to your ServiceNow record uses tabs (\t) as the delimiter between columns, instead of commas or semicolons which are more common.

 

  • Check the CSV File Format: Open your CSV file using a text editor like Notepad (Windows) or TextEdit (Mac). Ensure that each field within a row is separated by tabs (\t)  instead of commas.
  • Save the File with Tab Delimiters: If your CSV file is currently using commas as delimiters, you can convert it to use tabs by:
    • Opening the file in a spreadsheet program (e.g., Excel, Google Sheets).
    • Making sure the data is correctly formatted with tabs separating columns.
    • Saving or exporting the file in the "Tab Delimited Text" or "Tab Separated Values (TSV)" format.
  • Upload to ServiceNow: When attaching the CSV file to a record in ServiceNow (either through the UI or API), select the CSV file that you have confirmed uses tabs as delimiters. This ensures that ServiceNow interprets the file correctly when processing the attachment.

 

If this solution helps you , please hit the Thumb Icon and mark as Correct !!

 

Thankyou.