- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 11:39 PM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 11:55 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 12:46 AM - edited 09-12-2024 12:47 AM
// 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());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 11:55 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2024 11:59 PM
Script is giving error Can you provide a simple script without request, response objects
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-12-2024 12:46 AM - edited 09-12-2024 12:47 AM
// 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());
}