- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 12:48 AM
Use Case:
Attach CSV with tabs.
I know how to attach CSV but how to do tabs?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:04 AM - edited 07-03-2024 06:06 AM
To Generate CSV Files with Multiple Tabs in ServiceNow :
- 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.
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");
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 03:18 AM
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 09:38 AM
I have already implemented attaching CSV with table data, but the CSV with tabs is what I am querying here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 10:33 AM
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 01:34 AM
Sure, but the requirement is to have multiple tabs in an CSV/Excel.
Can you please elaborate on this one?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2024 02:32 AM - edited 07-02-2024 02:38 AM
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.