How to extract output of script in csv/xls ?

Snehal13
Kilo Sage

How to get data in csv or xls sheet after running below script. 

 

I want each row to have the respective incident number and priority as part of the sheet

 

var gr = new GlideRecord('incident'); 
gr.addEncodedQuery('work_notesLIKEscript is regression ready^ORwork_notesLIKEscript is production ready');
gr.query();
var count = gr.getRowCount();
gs.info("Found total "+count+" records");
while (gr.next()) {
    
    var category = gr.category;         
    var priority = gr.priority; 
    gs.info('Incident Number: ' + gr.number + 'for priority '+gr.priority); 
 
}

 

2 ACCEPTED SOLUTIONS

HIROSHI SATOH
Mega Sage

I don't think it's possible to output directly in Excel format because there is no library.

You can use the following code to create a CSV and download it after querying the incidents:

(function process(request, response) {
    // Initialize CSV content with headers
    var csvData = "Incident Number, Priority\n";

    // Query the incident table
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('work_notesLIKEscript is regression ready^ORwork_notesLIKEscript is production ready');
    gr.query();

    // Iterate through the records and append data to csvData
    while (gr.next()) {
        var incidentNumber = gr.getValue('number');
        var priority = gr.getValue('priority');
        csvData += incidentNumber + "," + priority + "\n";
    }

    // Set the response content type to CSV and trigger the download
    response.setContentType('text/csv');
    response.setHeader('Content-Disposition', 'attachment; filename="incidents.csv"');
    response.getWriter().write(csvData);
})(request, response);

 

View solution in original post

// Usage example: Export the 'incident' table with specific columns

var filename = tableName + "_" + new GlideDateTime().getNumericValue() + ".csv";

// Create a GlideSysAttachment record to store the CSV file
var attachment = new GlideSysAttachment();
var record = new GlideRecord('sys_attachment');
record.initialize();
record.table_name = 'sys_attachment';
record.table_sys_id = 'your_target_sys_id'; // Provide appropriate sys_id or target record
record.file_name = filename;
record.content_type = 'text/csv';
record.insert();

// Write the CSV content to the attachment
attachment.write(record.sys_id, filename, 'text/csv', new GlideText(csvContent).toString());
}

 

View solution in original post

3 REPLIES 3

HIROSHI SATOH
Mega Sage

I don't think it's possible to output directly in Excel format because there is no library.

You can use the following code to create a CSV and download it after querying the incidents:

(function process(request, response) {
    // Initialize CSV content with headers
    var csvData = "Incident Number, Priority\n";

    // Query the incident table
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery('work_notesLIKEscript is regression ready^ORwork_notesLIKEscript is production ready');
    gr.query();

    // Iterate through the records and append data to csvData
    while (gr.next()) {
        var incidentNumber = gr.getValue('number');
        var priority = gr.getValue('priority');
        csvData += incidentNumber + "," + priority + "\n";
    }

    // Set the response content type to CSV and trigger the download
    response.setContentType('text/csv');
    response.setHeader('Content-Disposition', 'attachment; filename="incidents.csv"');
    response.getWriter().write(csvData);
})(request, response);

 

Script is giving error Can you provide a simple script without request, response objects

// Usage example: Export the 'incident' table with specific columns

var filename = tableName + "_" + new GlideDateTime().getNumericValue() + ".csv";

// Create a GlideSysAttachment record to store the CSV file
var attachment = new GlideSysAttachment();
var record = new GlideRecord('sys_attachment');
record.initialize();
record.table_name = 'sys_attachment';
record.table_sys_id = 'your_target_sys_id'; // Provide appropriate sys_id or target record
record.file_name = filename;
record.content_type = 'text/csv';
record.insert();

// Write the CSV content to the attachment
attachment.write(record.sys_id, filename, 'text/csv', new GlideText(csvContent).toString());
}