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

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @ican 

 

Tabs? Are you referring to tabs in CSV or Tabs in SN?

and do you want to attach one csv to all records? ALot of information is missing.

*************************************************************************************************************
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 am referring to tabs in CSV, this i want to know how to do.

 

Thanks!

Hi @ican 

 

Looks big customization.  We need some parameter on form, which we can pass to CSV then write a code to verify the tab name with that parameter and then extract then in another csv and attach.

 

Not that easy and practical. What is use case of same? Where these csv are kept ?

*************************************************************************************************************
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]

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

The use case is a csv should be attached to an email with 2 tabs with table data.