Generate CSV file in specific format

dharani2607
Tera Contributor

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:

dharani2607_0-1687419928405.png

Can anyone please help

5 REPLIES 5

Ramkumar Thanga
Mega Sage

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

 

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?

dharani2607_0-1687421981778.png

 

Riya Verma
Kilo Sage
Kilo Sage

Hi @dharani2607 ,

 

Hope you are doing great.

To generate the CSV file in the desired format, you can follow these steps:

  1. Retrieve the data you need to include in the CSV file from the relevant tables or sources. 

  2. 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);

 

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Hello,

I have used this in BR, but csv is not getting generated,

 

(function executeRule(current, previous /*null when async*/ ) {
 
    var headers = ["Header1", "Header2", "Header3"];
var csvString = csvContent.map(row.join(",")).join("\n");
 
    var currentReq = current;
    var reqNumber = currentReq.number;
    //var fileName = "Request Details_Snow_" + reqNumber + ".csv";
    var fileName = reqNumber + ".csv";
 
    var csvData = ''; //The variable csvData will contain a string which is used to build the CSV file contents
    for (var i = 0; i < Headers.length; i++) { //Build the Headers
        csvData = csvData + '"' + Headers[i] + '"' + ',';
    }
    csvData = csvData + "\r\n";
 
    //attach the file to a record.
    var grRec = new GlideRecord("x_aukms_bally_request_details");
    grRec.addQuery("sys_id", current.sys_id);
    grRec.query();
    if (grRec.next()) {
        var grAttachment = new GlideSysAttachment();
        gs.info("Attachment added" + grAttachment);
        grAttachment.write(grRec, fileName, 'application/csv', csvData);
 
        geRec.comments = 'CSV file generated and attached';
 
    }
 
})(current, previous);
 
 
Am I missing something?
Just checked to get headers in csv like "header1","header2","header3"