Generate CSV file in specific format
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 12:45 AM
Hi All,
We need to generate the CSV file and attach to the request in specific format. I am able to attach csv in request but the format is not as expected.
Format:
Can anyone please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 12:53 AM
Hi !!
Could you please elaborate the problem you are facing.
Are you trying to attaching the CSV file to the request from the scripting part.
Thanks!!
Ramkumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 01:19 AM
No, I am able to attach the csv to request, but would require csv to be created in specific format.
How to customize the csv format?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 02:14 AM
Hi @dharani2607 ,
Hope you are doing great.
To generate the CSV file in the desired format, you can follow these steps:
Retrieve the data you need to include in the CSV file from the relevant tables or sources.
Construct the CSV file structure programmatically by defining the headers and the corresponding data.
var data = []; // Array to store the data rows
// Loop through your data source and populate the data array
// Example:
// for (var i = 0; i < dataSource.length; i++) {
// var row = [];
// row.push(dataSource[i].field1);
// row.push(dataSource[i].field2);
// // Add more fields as needed
// data.push(row);
// }
// Define the CSV file headers
var headers = ["Header1", "Header2", "Header3"];
// Combine headers and data into a single array
var csvContent = [headers].concat(data);
// Convert the array to CSV format
var csvString = csvContent.map(row => row.join(",")).join("\n");
- Later, you can attach it to the request using ServiceNow APIs.
var currentRequest = new GlideRecord('sc_request');
currentRequest.get(current.sys_id);
var attachment = new GlideSysAttachment();
attachment.write(currentRequest, 'filename.csv', 'text/csv', csvString);
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2023 06:05 AM
Hello,
I have used this in BR, but csv is not getting generated,